Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Showing native macOS alert window without using QGuiApplication
Forum Updated to NodeBB v4.3 + New Features

Showing native macOS alert window without using QGuiApplication

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 777 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abhishekm
    wrote on last edited by
    #1

    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 ?

    jeremy_kJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      2
      • SGaistS SGaist

        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.

        A Offline
        A Offline
        abhishekm
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          That's what the thread suggests at least.

          For iOS I do not know currently.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A abhishekm

            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 ?

            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #5

            @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?

            Asking a question about code? http://eel.is/iso-c++/testcase/

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved