[Qt-interest] QMainWindow: setWindowTitle() & setWindowIcon() don't work

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Fri Dec 12 11:53:33 CET 2008


Miguel Cardenas wrote on Thursday, December 11, 2008 6:39 PM:

> ...

Why do you pass in a pointer to the QApplication instance in here? QApplication is globally accessible anyway! Usually you would pass in a QWidget as parent (which for the top-level widget such as a QMainWindow would often be 0).

> MainWindow::MainWindow (QApplication* app) {
>    setWindowTitle(tr("some title"));
>    QApplication::setWindowIcon(QIcon(QPixmap(my_pixmap)));
>    ...
> }
> What is wrong in this code?

Where is your ui.setupUi(this); call?

I assume you have designed your MainWindow with the Qt designer and have a member variable 'ui', something like

  Ui_MainWindow ui;

The setupUi() call constructs the main window - including the icon and the title! Note that the title and icon are set to some default values in the Qt Designer, unless you change them.

So either

a) Change the title and icon in the Qt Designer or (if you need to set them dynamically)

b) Set the AFTER your call to ui.setupUi(this)

Something like this:

#include "ui_MainWindow.h" // generated by uic
class MainWindow : QMainWindow {
...

private:
  Ui_MainWindow ui;
}

MainWindow::MainWindow(QWidget *parent) {
  ...
  ui.setupUi(this);
  // only AFTER the window has been setup update the window title and icon
  setWindowTitle(tr("some title"));
  setWindowIcon(...);
  ...
}


Cheers, Oliver

--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22




More information about the Qt-interest-old mailing list