8

I want to change the default QMessageBox title to something else, so that I don't have to call setWindowTitle for every individual message box.

How is the default window title chosen?

Pieter
  • 31,619
  • 76
  • 167
  • 242
  • You may be better off creating a subclass of QMessageBox that sets the window title to what you want. – cgmb Apr 18 '11 at 18:55

4 Answers4

3

Best way to do this is to subclass QMessageBox, e.g.:

class MyMessageBox : public QMessageBox
{
   MyMessageBox()  //<-- default constructor 
   {
    setWindowTitle("Default title goes here"); //QMessageBox function
   }
};

Use MyMessageBox everywhere in the code.

JamesWebbTelescopeAlien
  • 3,547
  • 2
  • 30
  • 51
1

You could instead add a TARGET in the .pro file. e.g. add this line to the .pro file:

TARGET = MyApp

Thus "MyApp" will be applied both as the executable file name and also as default value for windowTitle of all QMessageBoxes in the entire project.

Lilla Eli
  • 271
  • 2
  • 3
  • 1
    You can also set app name with coding: `QApplication::setApplicationName` ( [`QCoreApplication::setApplicationName`](https://doc.qt.io/qt-5/qcoreapplication.html#applicationName-prop) ) or `QApplication::setApplicationDisplayName` ( [`QGuiApplication::setApplicationDisplayName`](https://doc.qt.io/qt-5/qguiapplication.html#applicationDisplayName-prop) ). – li ki Apr 20 '21 at 00:50
0

You don't need to call setWindowTitle method while you can title when you instance the QMessageBox object.

SIFE
  • 5,567
  • 7
  • 32
  • 46
  • I know I can pass along the preferred title in the constructor, but it's going to be the same everywhere so I thought I'd be better off changing the default window title to spare me a lot of copy/pasting. Would you suggest using a `const std::string` as the window title for all dialogs? This eliminates the copy/paste work, but it still requires me to indirectly state the window title in the constructor. – Pieter Apr 18 '11 at 18:57
  • Creating a function is usually a good way to eliminate duplicate code. –  Apr 18 '11 at 19:01
  • @Pieter First, I am not a C++ pro, so forgive me, all I can suggest is to make function as Mr Roku said and call it when you need it. – SIFE Apr 18 '11 at 19:14
-2

On Windows developing with VC2008 it takes the name from the project. Change the name of the project and it will change the title.