1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
use crate::QGuiApplication; use cpp_core::{MutPtr, MutRef}; use qt_core::QCoreApplicationArgs; use std::process; impl QGuiApplication { /// A convenience function for performing proper initialization and de-initialization of /// a Qt application. /// /// This function creates `CoreApplication` with valid `argc` and `argv`, calls the passed /// closure `f(app)` with the application object and exist the process with the exit code /// returned by the closure. The closure should perform the initialization of the application /// and either return immediately or call `CoreApplication::exec()` and return its return value: /// ```no_run /// use qt_gui::QGuiApplication; /// /// fn main() { /// QGuiApplication::init(|app| { /// unsafe { /// // initialization goes here /// QGuiApplication::exec() /// } /// }) /// } /// ``` pub fn init<F: FnOnce(::cpp_core::MutPtr<QGuiApplication>) -> i32>(f: F) -> ! { let exit_code = { unsafe { let mut args = QCoreApplicationArgs::new(); let (argc, argv) = args.get(); let mut app = QGuiApplication::new_2a( MutRef::from_raw(argc).unwrap(), MutPtr::from_raw(argv), ); f(app.as_mut_ptr()) } }; // drop `app` and `args` process::exit(exit_code) } }