Qt: set the main window title from everywhere

The scope of this article is about how to dynamically change the window’s title from anywhere. E.g. a text editor probably wants to show the file path in the title bar or if the file needs to saved it’s often marked as dirty and as soon as you’ve saved the document the dirty flag disappears. The title changes, however, can happen in different locations in your source code.

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(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, however, kind of implies there should only be one, so this simple check should be fine in most cases.

As an alternative you can use the macro qApp instead of the class QApplication which is an instance of that class too.

This means instead of

foreach(QWidget *widget, QApplication::topLevelWidgets()) { ... }

we could write the foreach as follows.

foreach(QWidget *widget, qApp->topLevelWidgets()) { ... }

The macro qApp is already defined in qapplication.h in Qt so there’s absolutely no need to define it yourself.

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 as well:

foreach(QWidget *widget, qApp->topLevelWidgets()) {
  if(widget->inherits("QMainWindow"))
    qobject_cast(widget)->setWindowTitle("-_-");
}

As the article’s title implies, you can put the foreach wherever you want to change the title, this mostly happens in a .cpp file.

If you simply want to change the default window title while it’s never getting updated during the runtime, you should edit the title in mainwindow.ui as highlighted in the following screenshot.

qtcreator

7 Comments

  1. I’m still new with Qt,I wanna ask where to put above code? in .cpp or main.cpp ,or header file?

  2. slopjong

    You could do something like

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        foreach(QWidget *widget, qApp->topLevelWidgets()) {
          if(widget->inherits("QMainWindow"))
            qobject_cast(widget)->setWindowTitle("-_-");
        }
    
        return a.exec();
    }
    

    but if you simply want to change the title statically, you should edit windowTitle in mainwindow.ui as highlighted in the screenshot in the article. The article’s scope is about how to dynamically change the window’s title from anywhere. E.g. a text editor probably wants to show the file path in the title bar or if the file needs to saved it’s often marked as dirty and as soon as you’ve saved the document the dirty flag disappears.

  3. Thank you slopjong…that is really helpful

  4. I got three error…what did I did wrong? Could you explain to me?

    C:\Users\arif\Desktop\Programming\Qt\HelloGui\main.cpp:14: error: C2784: ‘QSharedPointer<QtSharedPointer::RemovePointer::Type> qobject_cast(const QWeakPointer &)’ : could not deduce template argument for ‘const QWeakPointer &’ from ‘QWidget *’

    C:\Users\arif\Desktop\Programming\Qt\HelloGui\main.cpp:14: error: C2783: ‘T qobject_cast(const QObject *)’ : could not deduce template argument for ‘T’

    C:\Users\arif\Desktop\Programming\Qt\HelloGui\main.cpp:14: error: C2227: left of ‘->setWindowTitle’ must point to class/struct/union/generic type

  5. slopjong

    Is your source code available somewhere?

  6. Steven Samuel Cole

    much simpler: setWindowTitle("New Title");
    see also http://stackoverflow.com/a/14074631

  7. Hi blogger, i must say you have hi quality posts here.
    Your page can go viral. You need initial traffic only. How
    to get it? Search for; Mertiso’s tips go viral

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>