Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Showing native macOS alert window without using QGuiApplication



  • I am trying to show a NSAlert for macOS in my Qt Quick application. I want to show this alert before QGuiApplication has been instantiated. A bunch of internal things are done before QGuiApplication is instantiated. In case, any error occurs during these internal tasks, an alert message needs to be shown to the user.
    I am intending to implement the same using Objective C++. Inside my C++ code, I am calling an objective function that does the below:

    dispatch_async(dispatch_get_main_queue(), ^{
               NSAlert *alert = [[NSAlert alloc] init];
               [alert setMessageText:@"Delete this project?"];
               [alert setInformativeText:@"Deleted projects cannot be restored"];
               [alert addButtonWithTitle:@"OK"];
               [alert addButtonWithTitle:@"Cancel"];
               [alert setAlertStyle:NSWarningAlertStyle];
               [alert runModal];
        });
    

    The alert message still does not come up until i have a QGuiApplication instantiated and app.exec() running.

    Now the question is, if i do not instantiate Qt at all after the above code, will the alert box come up ?

    Is there something else that needs to be done as I am not able to get the alert message ?


  • Lifetime Qt Champion

    Hi,

    You are missing the basic parts that initialise a macOS application that QGuiApplication does for you in order for NSAlert to do it's job.

    You should check the last answer of this thread on the Apple developer forum for a possible alternative.



  • @SGaist Thanks. So is it safe to use CFUserNotificationDisplayAlert directly for macOS ?

    Can you also help with the equivalent experience for iOS ? Here again, on iOS, i would like to display an alert message to user without QGuiApplication.


  • Lifetime Qt Champion

    That's what the thread suggests at least.

    For iOS I do not know currently.



  • @abhishekm said in Showing native macOS alert window without using QGuiApplication:

    I am trying to show a NSAlert for macOS in my Qt Quick application. I want to show this alert before QGuiApplication has been instantiated. A bunch of internal things are done before QGuiApplication is instantiated. In case, any error occurs during these internal tasks, an alert message needs to be shown to the user.

    Is the QGuiApplication instance dependent on these internal tasks, or is there some reason that they must be performed first?


Log in to reply