Why doesn't QApplication offer a function mainWindow() ?
-
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'sQMainWindow
. Proposed solutions are horrible workarounds. Would the Qt developers consider adding a functionQMainWindow* QApplication::mainWindow()
or are there unsurmountable reasons why such a function cannot or should not exist?
-
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'sQMainWindow
. Proposed solutions are horrible workarounds. Would the Qt developers consider adding a functionQMainWindow* QApplication::mainWindow()
or are there unsurmountable reasons why such a function cannot or should not exist?
@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?
-
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'sQMainWindow
. Proposed solutions are horrible workarounds. Would the Qt developers consider adding a functionQMainWindow* QApplication::mainWindow()
or are there unsurmountable reasons why such a function cannot or should not exist?
@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
-
@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.Hilk well, then a function
QList<QMainWindow*> QApplication::mainWindows()
would be nice to have ...@Joachim-W there is
like @JonB said
https://doc.qt.io/qt-5/qapplication.html#topLevelWidgets -
@J.Hilk well, then a function
QList<QMainWindow*> QApplication::mainWindows()
would be nice to have ...@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 oftopLevelWidgets
, 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 areQMainWindow
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 aQWidget
, with some furniture :) I see that the solution to https://stackoverflow.com/questions/318641/multiple-qmainwindow-instances discusses this. -
@J.Hilk well, then a function
QList<QMainWindow*> QApplication::mainWindows()
would be nice to have ...@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(); }