Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Solved Why doesn't QApplication offer a function mainWindow() ?

    General and Desktop
    3
    7
    1552
    Loading More Posts
    • 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.
    • J
      Joachim W last edited by

      Since more than 10 years, the question has been discussed in many threads, here, at Stackoverflow, and elsewhere, how to get from qApp to the application's QMainWindow. Proposed solutions are horrible workarounds. Would the Qt developers consider adding a function

      QMainWindow* QApplication::mainWindow()
      

      or are there unsurmountable reasons why such a function cannot or should not exist?

      JonB J.Hilk 2 Replies Last reply Reply Quote 0
      • JonB
        JonB @Joachim W last edited by

        @Joachim-W
        I do understand you are asking about why Qt does not provide it. I use my own (Python/PyQt)

        def findMainWindow() -> typing.Union[QMainWindow, None]:
            # Global function to find the (open) QMainWindow in application
            app = QApplication.instance()
            for widget in app.topLevelWidgets():
                if isinstance(widget, QMainWindow):
                    return widget
            return None
        

        which is not too bad, just checking you know it can be as simple as that?

        1 Reply Last reply Reply Quote 3
        • J.Hilk
          J.Hilk Moderators @Joachim W last edited by

          @Joachim-W said in Why doesn't QApplication offer a function mainWindow() ?:

          or are there unsurmountable reasons why such a function cannot or should not exist?

          because having a QMainWindow is not mandatory for a QApplication ? Or even that there's only 1 QMainWindow instance

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

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

          J 1 Reply Last reply Reply Quote 5
          • J
            Joachim W @J.Hilk last edited by

            @J.Hilk well, then a function QList<QMainWindow*> QApplication::mainWindows() would be nice to have ...

            J.Hilk JonB 3 Replies Last reply Reply Quote 0
            • J.Hilk
              J.Hilk Moderators @Joachim W last edited by

              @Joachim-W there is
              like @JonB said
              https://doc.qt.io/qt-5/qapplication.html#topLevelWidgets

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

              Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

              1 Reply Last reply Reply Quote 6
              • JonB
                JonB @Joachim W last edited by JonB

                @Joachim-W
                I suspect that Qt does not need to keep a list of main windows for itself, so it's not going to offer one to you. It simply has a bunch of topLevelWidgets, main windows and others (e.g. closing the last of whatever these are induces program exit). Hence that code is all there is to detect which ones are QMainWindow instances.

                You & I may think there is something special about QMainWindow, and even that there should be only one of them, but to Qt they are just one more instance of a QWidget, with some furniture :) I see that the solution to https://stackoverflow.com/questions/318641/multiple-qmainwindow-instances discusses this.

                1 Reply Last reply Reply Quote 4
                • J.Hilk
                  J.Hilk Moderators @Joachim W last edited by

                  @Joachim-W here's a simple c++ adaptation of @JonB function,

                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      MainWindow w;
                      w.show();
                  
                      const QWidgetList &list = QApplication::topLevelWidgets();
                  
                      for(QWidget * w : list){
                          QMainWindow *mainWindow = qobject_cast<QMainWindow*>(w);
                          if(mainWindow)
                              qDebug() << "MainWindow found" << w;
                      }
                  
                      return a.exec();
                  }
                  

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

                  Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

                  1 Reply Last reply Reply Quote 5
                  • First post
                    Last post