4
votes

Look at the following Image:

I have created the Sub Window dynamically.

I'm tried to use setWindowIcon function like the following:

mdiWindows->setWindowIcon(QIcon("icon.ico"));

But does not works fine.

Also see the following code (MDI window creation):

QWidget *widget = new QWidget(this);
QTextEdit *TextEdit = new QTextEdit(widget);
TextEdit->setObjectName("myTextEdit");
QMdiSubWindow *mdiWindows = ui->mdiArea->addSubWindow(widget);
mdiWindows->setGeometry(5, 5, 300, 250);
mdiWindows->setWindowTitle("untitled" + QString::number(ui->mdiArea->subWindowList().count()));
mdiWindows->setWindowState(Qt::WindowMaximized);
mdiWindows->layout()->addWidget(TextEdit);
mdiWindows->layout()->setContentsMargins(0,0,0,
mdiWindows->layout()->setSpacing(
mdiWindows->show();

How to change MDI subWindow icon ?

2
The icon displayed in the MDI child windows can be edited in the properties of the sub-application object used to created the window. "Window icon" property.pes502
@pes502: I need an example to be compatible with my code, please.Lion King
Have you tried changing the icon with the setWindowIcon function?RobbieE
@RobbieE: I tried to use mdiWindows->setWindowIcon(QIcon("icon.ico"));, but does not works fine.Lion King
setWindowIcon does what you need. If you cannot get it to work, how about you do some debugging? What does "does not works fine" mean? How does it fail. Does QIcon("icon.ico") give you a valid icon?David Heffernan

2 Answers

3
votes

What's wrong?

I'm tried to use setWindowIcon function like the following: mdiWindows->setWindowIcon(QIcon("icon.ico"));

But you have done wrong, because:

  1. You set icon on mdiWindow itself rather than it's subWindow.
  2. Besides, .ico is for Application icon in Windows, you should just use .jpg or .png format. The details of default supporting format list can be found here.

(If you insist on .ico file, there is a workaround. Check: ".ico icons not showing up on Windows")


Solution:

Therefore, change this line mdiWindows->setWindowIcon(QIcon("icon.ico"));

into: widget->setWindowIcon(QIcon(":/myIcon/icon.png"));

(Notice that you can do the same on other QWidget derivatives: QMainWindow, QDialog...etc to set their window icon)

In other words, insert the above line into your code:

//QWidget *widget = new QWidget(this);
//QTextEdit *TextEdit = new QTextEdit(widget);
//TextEdit->setObjectName("myTextEdit");
widget->setWindowIcon(QIcon(":/myIcon/icon.png")); 
//QMdiSubWindow *mdiWindows = ui->mdiArea->addSubWindow(widget);
//mdiWindows->setGeometry(5, 5, 300, 250);
//mdiWindows->setWindowTitle("untitled" + QString::number(ui->mdiArea->subWindowList().count()));
//mdiWindows->setWindowState(Qt::WindowMaximized);
//mdiWindows->layout()->addWidget(TextEdit);
//mdiWindows->layout()->setContentsMargins(0,0,0,
//mdiWindows->layout()->setSpacing(
//mdiWindows->show();

enter image description here


P.S.

Just in case, if you want to set them later, you can call QMdiArea::subWindowList() to get the list of mdiWindows then set icons on them separately. For example:

mdiWindows->subWindowList().at(1)->setWindowIcon(QIcon(":/myIcon/icon.png"));

This works the same.

0
votes

I could not get this accepted answer to work. Don't know if it's a version-specific bug, but I'm using PyQt5,

from PyQt5.QtCore import QT_VERSION_STR
from PyQt5.Qt import PYQT_VERSION_STR
print(f"QT: {QT_VERSION_STR}, PYQT: {PYQT_VERSION_STR}")
# QT: 5.9.6, PYQT: 5.9.2

The workaround for me was to set the icon on the QMdiSubWindow directly and not the widget.

mdiArea = ...
widget = ...
subwindow = mdiArea.addSubWindow(widget)
subwindow.setWindowTitle("My Widget")
myicon = ...
subwindow.setWindowIcon(myicon)  # <-- this line
widget.show()

Note, it seems, at least on Windows, that the icon must be a .ico file, using .png did not work, at least for me.