如何更改 MDI 子窗口图标? [英] How to change MDI subWindow icon?

查看:81
本文介绍了如何更改 MDI 子窗口图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看下图:

我动态创建了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"));

但是你做错了,因为:

  1. 您在 mdiWindow 本身而不是它的子窗口上设置图标.
  2. 此外,.icoWindows 中的应用程序图标,您应该只需使用 .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?

    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();
    


    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 子窗口图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆