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