问题描述
看下图:
我动态创建了Sub Window
.
我尝试使用 setWindowIcon
函数,如下所示:
mdiWindows->setWindowIcon(QIcon("icon.ico"));
但效果不佳.
另见以下代码(MDI窗口创建):
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();
如何更改 MDI 子窗口图标?
怎么了?
我尝试使用 setWindowIcon
函数,如下所示:mdiWindows->setWindowIcon(QIcon("icon.ico"));
但是你做错了,因为:
- 您在 mdiWindow 本身而不是它的子窗口上设置图标.
- 此外,
.ico
是 Windows 中的应用程序图标,您应该只需使用.jpg
或.png
格式.默认支持格式列表的详细信息可以在 获取 mdiWindows 列表,然后分别在它们上设置图标.例如:mdiWindows->subWindowList().at(1)->setWindowIcon(QIcon(":/myIcon/icon.png"));
效果一样.
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 ?
解决方案What's wrong?
But you have done wrong, because:
- You set icon on mdiWindow itself rather than it's subWindow.
- Besides,
.ico
is for Application icon in Windows, you shouldjust 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();
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.
这篇关于如何更改 MDI 子窗口图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!