In Qt the title of the main window can be set from everywhere and thankfully to the QApplication class we gain access to all created widgets by using the method findChildren() or topLevelWidgets(). The difference between both is that findChildren() returns a list only with widgets which reside inside another widget and in contrast topLevelWidgets() returns a list with parentless widgets which are always independent windows or dialogs.
With this background knowledge topLevelWidgets() is the way to go because findChildren only finds… children.
As a showing example here is a code snippet:
foreach(QWidget *widget, QApplication::topLevelWidgets()) {
if(widget->objectName() == "MainWindow")
qobject_cast<QMainWindow*>(widget)->setWindowTitle("-_-");
}
In the foreach loop we walk through the whole list of top-level widgets and because we want to change the title of the main window, we check the widget’s object name. In this example the main window’s object name is ‘MainWindow’ which is the default in Qt Designer.
Object names are not necessarily unique, multiple objects can have the same object name and thus the wrong widget could be chosen. MainWindow kind of implies there should only be one, so this simple check should be fine in most cases.
An alternative to the class QApplication is the macro qApp which is an instance of that class.
#define qApp (static_cast<QApplication *>(QCoreApplication::instance()))
Because QApplication implements the singleton design pattern only one instance can exist at most. If we are sure that there are no other objects of the type QMainWindow in the top-level widget list we could also check if it inherits from that class.
In place of the code above this would be fine then as well:
foreach(QWidget *widget, qApp->topLevelWidgets()) {
if(widget->inherits("QMainWindow"))
qobject_cast<QMainWindow*>(widget)->setWindowTitle("-_-");
}