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. Why doesn't QApplication offer a function mainWindow() ?

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

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 3.5k 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.
  • J Offline
    J Offline
    Joachim W
    wrote on last edited by
    #1

    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?

    JonBJ J.HilkJ 2 Replies Last reply
    0
    • J Joachim W

      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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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
      3
      • J Joachim W

        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?

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

        @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


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

        J 1 Reply Last reply
        5
        • J.HilkJ J.Hilk

          @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

          J Offline
          J Offline
          Joachim W
          wrote on last edited by
          #4

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

          J.HilkJ JonBJ 3 Replies Last reply
          0
          • J Joachim W

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

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

            @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


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

            1 Reply Last reply
            6
            • J Joachim W

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @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
              4
              • J Joachim W

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

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

                @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


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

                1 Reply Last reply
                5

                • Login

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