Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Change QGraphicsProxyWidget window icon
Forum Updated to NodeBB v4.3 + New Features

Change QGraphicsProxyWidget window icon

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    danifujii
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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()));
      
      D 1 Reply Last reply
      1
      • Chris KawaC Chris Kawa

        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()));
        
        D Offline
        D Offline
        danifujii
        wrote on last edited by
        #3

        @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!

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved