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. Positioning QMessageBox
Forum Updated to NodeBB v4.3 + New Features

Positioning QMessageBox

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 7.3k Views 1 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.
  • bart.hollisB Offline
    bart.hollisB Offline
    bart.hollis
    wrote on last edited by
    #1

    Is there a way to make sure QMessageBox is displayed on top of and within the confines of the main window?

    Bart

    K J.HilkJ 2 Replies Last reply
    0
    • bart.hollisB bart.hollis

      Is there a way to make sure QMessageBox is displayed on top of and within the confines of the main window?

      Bart

      K Offline
      K Offline
      kenchan
      wrote on last edited by
      #2

      @bart.hollis
      Can't you make it a modal dialog?

      1 Reply Last reply
      0
      • bart.hollisB bart.hollis

        Is there a way to make sure QMessageBox is displayed on top of and within the confines of the main window?

        Bart

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @bart.hollis that should be the case, if you give your QMessageBox the "Main Window" as parent.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        0
        • bart.hollisB Offline
          bart.hollisB Offline
          bart.hollis
          wrote on last edited by
          #4

          Would you please explain how I do that? I can't seem to get the parameters correct.

          J.HilkJ 1 Reply Last reply
          0
          • bart.hollisB bart.hollis

            Would you please explain how I do that? I can't seem to get the parameters correct.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #5

            @bart.hollis
            Too few information to give a concrete example. But something like this:

            void MainWindow::showMessageBox() {
                QMessageBox *msg = new QMessageBox::about(this, "MsgBoxTitel", "Display Text");
                msg->exec();
            }
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            1
            • bart.hollisB Offline
              bart.hollisB Offline
              bart.hollis
              wrote on last edited by
              #6

              This is the code I'm using. It is contained in the MainWindow.cpp file. I've tried all I can think of for arguments to the setParent() . What's there is the end of a long list of things I have tried. Would certainly appreciate being set straight!

              void MainWindow::on_tabWidget_tabBarClicked(int index)
              {
                  static bool firstTime = true;
                  if (firstTime) {
                      firstTime = false;
                      // If no data in files, ask to create sample.
                      if (isEmpty())
                      {
                          QMessageBox msgBox;
                          msgBox.setIcon(QMessageBox::Information);
                          //msgBox.setParent(QWidget Ui->QMainWindow);
                          msgBox.setGeometry(450, 450, 250, 200);
                          msgBox.setText("Notice! \n There's nothing in the tables!");
                          msgBox.setInformativeText("Wanna add some sample stuff?");
                          msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
                          msgBox.setDefaultButton(QMessageBox::No);
                          msgBox.setEscapeButton(QMessageBox::No);
                          if (msgBox.exec() == QMessageBox::Yes)
                              addSampleData();
                      }
                  }
              
              J.HilkJ K 2 Replies Last reply
              0
              • bart.hollisB bart.hollis

                This is the code I'm using. It is contained in the MainWindow.cpp file. I've tried all I can think of for arguments to the setParent() . What's there is the end of a long list of things I have tried. Would certainly appreciate being set straight!

                void MainWindow::on_tabWidget_tabBarClicked(int index)
                {
                    static bool firstTime = true;
                    if (firstTime) {
                        firstTime = false;
                        // If no data in files, ask to create sample.
                        if (isEmpty())
                        {
                            QMessageBox msgBox;
                            msgBox.setIcon(QMessageBox::Information);
                            //msgBox.setParent(QWidget Ui->QMainWindow);
                            msgBox.setGeometry(450, 450, 250, 200);
                            msgBox.setText("Notice! \n There's nothing in the tables!");
                            msgBox.setInformativeText("Wanna add some sample stuff?");
                            msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
                            msgBox.setDefaultButton(QMessageBox::No);
                            msgBox.setEscapeButton(QMessageBox::No);
                            if (msgBox.exec() == QMessageBox::Yes)
                                addSampleData();
                        }
                    }
                
                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #7

                @bart.hollis
                I don't know of an easy way to confine the MessageBox inside the parent Window, you would have to subclass it or make your totaly own MessageBox.

                But here's the closest adjusment of a standard QMessageBox I could come up with:

                QMessageBox *msgBox = new QMessageBox(this);
                    msgBox->setIcon(QMessageBox::Information);
                    msgBox->resize(250, 200);
                    msgBox->setText("Notice! \n There's nothing in the tables!");
                    msgBox->setInformativeText("Wanna add some sample stuff?");
                    msgBox->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
                    msgBox->setDefaultButton(QMessageBox::No);
                    msgBox->setEscapeButton(QMessageBox::No);
                    msgBox->setWindowFlag(Qt::FramelessWindowHint,true);
                    msgBox->setStyleSheet("QMessageBox{border: 1px solid black; background-color:white}");
                
                    if (msgBox->exec() == QMessageBox::Yes)
                                addSampleData();
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                3
                • bart.hollisB bart.hollis

                  This is the code I'm using. It is contained in the MainWindow.cpp file. I've tried all I can think of for arguments to the setParent() . What's there is the end of a long list of things I have tried. Would certainly appreciate being set straight!

                  void MainWindow::on_tabWidget_tabBarClicked(int index)
                  {
                      static bool firstTime = true;
                      if (firstTime) {
                          firstTime = false;
                          // If no data in files, ask to create sample.
                          if (isEmpty())
                          {
                              QMessageBox msgBox;
                              msgBox.setIcon(QMessageBox::Information);
                              //msgBox.setParent(QWidget Ui->QMainWindow);
                              msgBox.setGeometry(450, 450, 250, 200);
                              msgBox.setText("Notice! \n There's nothing in the tables!");
                              msgBox.setInformativeText("Wanna add some sample stuff?");
                              msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
                              msgBox.setDefaultButton(QMessageBox::No);
                              msgBox.setEscapeButton(QMessageBox::No);
                              if (msgBox.exec() == QMessageBox::Yes)
                                  addSampleData();
                          }
                      }
                  
                  K Offline
                  K Offline
                  kenchan
                  wrote on last edited by kenchan
                  #8

                  @bart.hollis
                  When you say "QMessageBox is displayed on top of and within the confines of the main window" do you mean,

                  1. it should always appear above and remain on top of the parent window until the user dismisses it. Further, it should not go behind if the use happens to click on the parent window?
                  2. the use should not be able to drag it out of "the confines" of the parent window?

                  (1.) should happen if you give it the parent widgets pointer in the constructor. Message boxes are supposed to be modal by default.
                  (2.) can be done if as @J-Hilk suggests you subclass the message box and override the mouse events. Do you really want go that far?

                  Also why bother using pointer and new for the message box? It looks like having one on the stack is all you need.
                  I find using resize() and move() is usually better that setGeometry() for positioning windows and dialogs :-).

                  bart.hollisB 1 Reply Last reply
                  0
                  • K kenchan

                    @bart.hollis
                    When you say "QMessageBox is displayed on top of and within the confines of the main window" do you mean,

                    1. it should always appear above and remain on top of the parent window until the user dismisses it. Further, it should not go behind if the use happens to click on the parent window?
                    2. the use should not be able to drag it out of "the confines" of the parent window?

                    (1.) should happen if you give it the parent widgets pointer in the constructor. Message boxes are supposed to be modal by default.
                    (2.) can be done if as @J-Hilk suggests you subclass the message box and override the mouse events. Do you really want go that far?

                    Also why bother using pointer and new for the message box? It looks like having one on the stack is all you need.
                    I find using resize() and move() is usually better that setGeometry() for positioning windows and dialogs :-).

                    bart.hollisB Offline
                    bart.hollisB Offline
                    bart.hollis
                    wrote on last edited by
                    #9

                    @kenchan said in Positioning QMessageBox:

                    @bart.hollis
                    When you say "QMessageBox is displayed on top of and within the confines of the main window" do you mean,

                    1. it should always appear above and remain on top of the parent window until the user dismisses it. Further, it should not go behind if the use happens to click on the parent window?
                    2. the use should not be able to drag it out of "the confines" of the parent window?

                    (1.) should happen if you give it the parent widgets pointer in the constructor. Message boxes are supposed to be modal by default.
                    (2.) can be done if as @J-Hilk suggests you subclass the message box and override the mouse events. Do you really want go that far?

                    Also why bother using pointer and new for the message box? It looks like having one on the stack is all you need.
                    I find using resize() and move() is usually better that setGeometry() for positioning windows and dialogs :-).

                    J. Hilk, Thank you for your reply. Yes to your point 1. It should always appear above and remain on top of the parent window. Don't really care if the user drags it out of the location, just want it to appear there. Your remark about giving it the parent wigets pointer is what I understand I should do. I just don't know how! I know this is a simple C++ problem, and I've studied and tried everything I can see, but just can't seem to figure this out. I guess my question should really be, How, exactly, do I give the pointer to the parent wiget? What is an example?

                    1 Reply Last reply
                    0
                    • bart.hollisB Offline
                      bart.hollisB Offline
                      bart.hollis
                      wrote on last edited by
                      #10

                      I Found it! The pointer it was looking for is "this"! "this" is the pointer to the parent widget. Thanks for all who helped with this simple, but frustrating problem. I hate being a noob! :)
                      Bart

                      K 1 Reply Last reply
                      2
                      • bart.hollisB bart.hollis

                        I Found it! The pointer it was looking for is "this"! "this" is the pointer to the parent widget. Thanks for all who helped with this simple, but frustrating problem. I hate being a noob! :)
                        Bart

                        K Offline
                        K Offline
                        kenchan
                        wrote on last edited by
                        #11

                        @bart.hollis Glad to hear you found it :-).
                        Yes if the function from which you create the object is itself a member function of an object the pointer to itself is called "this" in c++.

                        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