Change QGraphicsProxyWidget window icon



  • Hi guys.
    I need some help or suggestions. I have to add a "Designer form" class to a scene (i need to be able to move these windows in the scene). I do it through

    scene->addWidget(mainWindowSubClass, Qt::Window);
    

    The thing is, this returns a QGraphicsProxyItem. I can only change it's title but not its icon, which is by default the Qt one. I definitely want to change that.
    What can I do?
    In the "mainWindowSubClass" I have a toolbar and a listWidget below it.

    Thanks!


  • Moderators

    Hi, welcome to devnet.

    AFAIK there's no way exposed in the proxy widget, but there's another - you can create a proxy style to return your icon:

    struct CustomIconProxy : public QProxyStyle {
        CustomIconProxy(QStyle *style = nullptr) : QProxyStyle(style) { }
        QIcon standardIcon(StandardPixmap icon, const QStyleOption* o = nullptr, const QWidget* w = nullptr) const override {
            if(icon == SP_TitleBarMenuButton)
                return /* your icon here */;
            else
                return QProxyStyle::standardIcon(icon, o, w);
        }
    };
    

    and set it like so:

    auto proxyWidget = scene->addWidget(mainWindowSubClass, Qt::Window);
    proxyWidget->setStyle(new CustomIconProxy(style()));
    


  • @Chris-Kawa It works perfectly. Thank you! I think this is going to help people because I looked for this info and found nothing.
    One more thing, is there any chance to change the window frame color or style with this same thing? I am in Windows 10 and it uses like a Windows 7 one. Although if i could change the colors it would look good too.

    Thanks!


  • Moderators

    Qt uses mdi window style for drawing proxy widgets.
    As weird as it may sound this is exactly what mdi window frame looks like, even in Windows 10.
    So the "native" thing to do would be to leave it as it is.

    If you still want to customize the frame you can use the above proxy style. The class methods of interest would be drawPrimitive and drawComplexControl. To give you a head start here is a dummy implementation of them that you can expand on:

    void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w = 0) const override
    {
        if (pe == QStyle::PE_FrameWindow)
            p->fillRect(opt->rect, Qt::red);
        else
            QProxyStyle::drawPrimitive(pe, opt, p, w);
    }
    
    void drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget = 0) const override
    {
        if (cc == QStyle::CC_TitleBar)
            p->fillRect(opt->rect, Qt::blue);
        else
            QProxyStyle::drawComplexControl(cc, opt, p, widget);
    }
    

    There's some more work needed to draw the title bar buttons, but you can check the base implementation to see how it's done.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.