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. Centering a QMessageBox relative to the main window?

Centering a QMessageBox relative to the main window?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 8.4k 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.
  • P Offline
    P Offline
    pwizard
    wrote on last edited by
    #1

    In my program I have several C++ classes that are Qt-based (that is, they use QObjects instead of generic C++ objects) but lack user interfaces of their own. I use QMessageBoxes in the error handling code in these classes to alert users if an error occurs.

    When I'm working with these classes, I can't use this as a parent since there is no interface to center the message box against. The only way I can get the code to compile in such instances is to use NULL as a parent but that causes the message boxes to be centered on the screen instead of relative to the main window. It works but it looks really strange if the main window isn't positioned close to the center of the screen.

    Is there a way to force these message boxes to center relative to the app's main window? The most logical way to fix this problem would be to create a pointer to the active mainwindow object to use as a parent but I'm not quite sure how to do it. Any suggestions?

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

      Hi,

      What about "this":http://qt-project.org/doc/qt-4.8/qapplication.html#activeWindow ?

      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
      • P Offline
        P Offline
        pwizard
        wrote on last edited by
        #3

        Didn't work. I just tried
        @
        QWidget* mw=QApplication::activeWindow();

        QMessageBox m;
        m.critical(mw, "blah", "blah blah");@

        and the message boxes are still centering on the screen. It looks like that method requires the main window to have focus at the time, so maybe it's not working for me because a QDialog initiates this code.

        i.e.

        QMainWindow --> QDialog --> Another class that contains this code

        instead of

        QMainWindow --> Another class that contains this code

        Since that QDialog is destroyed by the time this code is executed, it looks like QApplication::activeWindow() is returning nothing (unless the focus automatically returns to the main window once the QDialog is gone).

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pwizard
          wrote on last edited by
          #4

          I also unsuccessfully tried mw->parentWidget() to see if mw was pointing to the QDialog. It compiles but the app crashes when the program flow gets to that point (that pointer must be null if mw is returning 0).

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            if you use QMessageBox' static methods to display it you wont be able to access them. Since you wont get a pointer to work with the respecting widget.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pwizard
              wrote on last edited by
              #6

              @raven-worx: I don't understand. Are you saying there is no way to create a pointer to the active QMainWindow object?

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

                raven-worx just said that you are using critical the wrong way, it's a static function

                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
                • P Offline
                  P Offline
                  pwizard
                  wrote on last edited by
                  #8

                  A static object was indeed the right way to do this. I solved this problem by creating a public static pointer to the main window object in the MainWindow class:

                  @ static QWidget* mw;@

                  and setting the pointer to

                  @ mw=this->window();@

                  in the main window constructor. By including MainWindow in all classes that need this type of parent, I can now properly center dialogs in classes without UIs by using MainWindow::mw as the parent.

                  Thanks for the help!

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

                    QApplication::topLevelWidgets() might be cleaner

                    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
                    • S Offline
                      S Offline
                      s4nt14g0
                      wrote on last edited by s4nt14g0
                      #10

                      Hi, I just solved this by inheriting from the main window.
                      I am using PyQt5 (Python)

                      QMessageBox(W)

                      where
                      W is your variable initiating the a class that inherits from QMainWindow.

                      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