ChatGPT解决这个技术问题 Extra ChatGPT

How to change the Title of the window in Qt?

How to change the title of the window in Qt? (Both for QDialog and QMainWindow.)


C
Community
void    QWidget::setWindowTitle ( const QString & )

EDIT: If you are using QtDesigner, on the property tab, there is an editable property called windowTitle which can be found under the QWidget section. The property tab can usually be found on the lower right part of the designer window.


Just for completeness here is it explained: qt-project.org/doc/qt-4.8/qwidget.html#windowTitle-prop
I dont know about other people, but stackoverflow is becoming a very useful documentation alternative, google often points me here before the actual documentation (and its also easier to read).
@chacham15 The Qt documentation is quite good and very easy to read. SO doesn't even come close for simple what-is-the-function-name questions; we do better at slightly more complicated problems.
Maybe good to know: better put the "setWindowTitle()" at the end of the constructor
Is there a way to change font-family of the window title? Also can we make it appear bold?
B
BaCaRoZzo

For new Qt users this is a little more confusing than it seems if you are using QT Designer and .ui files.

Initially I tried to use ui->setWindowTitle, but that doesn't exist. ui is not a QDialog or a QMainWindow.

The owner of the ui is the QDialog or QMainWindow, the .ui just describes how to lay it out. In that case, you would use:

this->setWindowTitle("New Title");

I hope this helps someone else.


@UmNyobe The code generated from that .ui file will call Owner->setWindowTitle(), so it's no different. If you want the window title to be dynamic, you'd do it this way rather than in the .ui file.
+1 for giving an example of actually implementing this in the code.
Note that this method is not protected and can be called from outside the dialog class as well.
b
bandito40

I know this is years later but I ran into the same problem. The solution I found was to change the window title in main.cpp. I guess once the w.show(); is called the window title can no longer be changed. In my case I just wanted the title to reflect the current directory and it works.

int main(int argc, char *argv[]) 
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowTitle(QDir::currentPath());
w.show();

return a.exec();
}

K
KingKong

You can also modify the windowTitle attribute in Qt Designer.


e
eyllanesc
system("title WhateverYouWantToNameIt");

You should put a brief description of your answer.
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
Does this work? Is this even Qt? I never heard of this function.