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. Display message box on top of ui in the beginning
Forum Update on Monday, May 27th 2025

Display message box on top of ui in the beginning

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 393 Views
  • 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.
  • S Offline
    S Offline
    shreya_agrawal
    wrote on last edited by
    #1

    I want to display a QMessageBox as soon as I run the application. But, I want it to be displayed on top of the ui. For this, I wrote the following code in my mainWindow constructor:

    loadJSONData("/path/to/json");
    
    if(!flag)
    {
        ui->setupUi(this);
        QMessageBox msgBox;
        msgBox.setWindowTitle("Error");
        msgBox.setText("The flag is false. Do you still want to continue.");
        msgBox.setStandardButtons(QMessageBox::Ok);
        QAbstractButton *okButton = msgBox.button(QMessageBox::Ok);
        okButton->setText("Exit");
        msgBox.setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint);
        msgBox.exec();
        if(msgBox.clickedButton() == okButton)
        {
            QApplication::quit();
            return;
        }
    }
    else
    {
        ui->setupUi(this);
    }
    

    When I run the application, I first see the QMessageBox and then on clicking the exit button (in the QMessageBox), the blank ui is visible. Instead, I want that the QMessage box should be displayed on top of a blank ui and the application should quit on clicking the exit button.

    Note: We obtain the value of the flag from loadJSONData() function.

    jsulmJ Pl45m4P 2 Replies Last reply
    0
    • S shreya_agrawal

      I want to display a QMessageBox as soon as I run the application. But, I want it to be displayed on top of the ui. For this, I wrote the following code in my mainWindow constructor:

      loadJSONData("/path/to/json");
      
      if(!flag)
      {
          ui->setupUi(this);
          QMessageBox msgBox;
          msgBox.setWindowTitle("Error");
          msgBox.setText("The flag is false. Do you still want to continue.");
          msgBox.setStandardButtons(QMessageBox::Ok);
          QAbstractButton *okButton = msgBox.button(QMessageBox::Ok);
          okButton->setText("Exit");
          msgBox.setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint);
          msgBox.exec();
          if(msgBox.clickedButton() == okButton)
          {
              QApplication::quit();
              return;
          }
      }
      else
      {
          ui->setupUi(this);
      }
      

      When I run the application, I first see the QMessageBox and then on clicking the exit button (in the QMessageBox), the blank ui is visible. Instead, I want that the QMessage box should be displayed on top of a blank ui and the application should quit on clicking the exit button.

      Note: We obtain the value of the flag from loadJSONData() function.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @shreya_agrawal Don't create the message box in main window constructor - at that time main window is not fully constructed and is not shown. Do it in your main where you create the main window: create and show main window, then create and show the message box.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      5
      • S shreya_agrawal

        I want to display a QMessageBox as soon as I run the application. But, I want it to be displayed on top of the ui. For this, I wrote the following code in my mainWindow constructor:

        loadJSONData("/path/to/json");
        
        if(!flag)
        {
            ui->setupUi(this);
            QMessageBox msgBox;
            msgBox.setWindowTitle("Error");
            msgBox.setText("The flag is false. Do you still want to continue.");
            msgBox.setStandardButtons(QMessageBox::Ok);
            QAbstractButton *okButton = msgBox.button(QMessageBox::Ok);
            okButton->setText("Exit");
            msgBox.setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint);
            msgBox.exec();
            if(msgBox.clickedButton() == okButton)
            {
                QApplication::quit();
                return;
            }
        }
        else
        {
            ui->setupUi(this);
        }
        

        When I run the application, I first see the QMessageBox and then on clicking the exit button (in the QMessageBox), the blank ui is visible. Instead, I want that the QMessage box should be displayed on top of a blank ui and the application should quit on clicking the exit button.

        Note: We obtain the value of the flag from loadJSONData() function.

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

        @shreya_agrawal

        Having a button named OK button with text saying "Exit" to quit the app is very confusing...


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        S 1 Reply Last reply
        0
        • jsulmJ jsulm

          @shreya_agrawal Don't create the message box in main window constructor - at that time main window is not fully constructed and is not shown. Do it in your main where you create the main window: create and show main window, then create and show the message box.

          S Offline
          S Offline
          shreya_agrawal
          wrote on last edited by shreya_agrawal
          #4

          @jsulm
          Thank you for your reply!
          Yes, it worked by creating it in the main!

          1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @shreya_agrawal

            Having a button named OK button with text saying "Exit" to quit the app is very confusing...

            S Offline
            S Offline
            shreya_agrawal
            wrote on last edited by
            #5

            @Pl45m4
            Yes, you are right. I was just testing the display of the message box for now. Thanks for pointing it out though.

            1 Reply Last reply
            0
            • S shreya_agrawal has marked this topic as solved on

            • Login

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