Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Don't get custom QStyle to do any effect



  • Hello,

    I am new to QT development and trying to do a customer style. As base of my work I used the NorvegianWood style example from QT using QT 6.0.2.

    Currently I do only set the texture for the Window and Button roles having a test window with several organizational widgets (layouts, QWidget and a scrolling area) as well as a single push button.

    I would at least expect the texture of the button to change, but nothing happens. The style itself seems to be taken into account because if I remove the QApplication::setStyle the style changes (my customer style being based on the "Windows" QProxyStyle).

    However when it comes to the palette it looks like the controls still use the proxy styles palette, I have debugged calls to QPalette::brush() after the palette is setup. My palette shows in the relevant d.br[][] array a "texture" brush type, while QPalette::brush() gets a "solid" brush type in the same place.

    SetTexture() has been taken as-is from the QT example, setting all color groups at this time.

    The mainindow was designed in QT Creator and the TMainWindow class generated from an ui file, if would matter.

    What am I missing ?

    int main(int argc, char *argv[])
    {
        Q_INIT_RESOURCE(my_style_resources);
    
        QApplication::setStyle(new TMyStyle());
    
        QApplication a(argc, argv);
        TMainWindow w;
    
    
        ...
    
        w.show();
        return a.exec();
    }
    
    ///////////////////////////////////////////////////////////////////////
    class TMyStyle: public QProxyStyle {
      Q_OBJECT
    public:
      TMyStyle():QProxyStyle(QStyleFactory::create("windows")),
        p_standardPalette(nullptr)
      {
      }
      virtual ~TMyStyle() {};
    
      QPalette standardPalette() const override;
    
    protected:
    private:
      mutable QPalette _standardPalette;
      mutable QPalette *p_standardPalette;
      static void setTexture(QPalette &palette, QPalette::ColorRole role,
                             const QImage &image);
    };
    
    QPalette TMyStyle::standardPalette() const
    {
      if(p_standardPalette==nullptr)
      {
        QColor yellow(245, 210, 86);
    
        QPalette palette(yellow);
    
        QImage backgroundImage(":/img/weave-back.jpg");
        QImage buttonImage(":/img/weave-button.jpg");
    
        setTexture(palette, QPalette::Window, backgroundImage);
        setTexture(palette, QPalette::Button, buttonImage);
    
        _standardPalette=palette;
        p_standardPalette = &_standardPalette; // Mark ready
      }
    
      return _standardPalette;
    }
    
    //--------------------------------------------------------------------------------
    void TMyStyle::setTexture(QPalette &palette, QPalette::ColorRole role, const QImage &image)
    {
      for (int i = 0; i < QPalette::NColorGroups; ++i) {
        QBrush brush(image);
        brush.setColor(palette.brush(QPalette::ColorGroup(i), role).color());
        palette.setBrush(QPalette::ColorGroup(i), role, brush);
      }
    }
    


  • Ok, I found the reason, and a workaround but do still not understand why the NorvegianWood example does not have this issue.

    The reason is that QAppklication::basePalette() calls the styles standardPalette() as expected but the returned palette is then overridden by the theme palette.

    Later the method calls the styles polish(QPalette &pal) again and as a workaround I assigned my palette to pal again, and then it works as expected.

    I sense this may not the proper solution ? Especially as, as far as I can see, NorveigianWood does the same without overriding polish(QPalette &).


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you show your style code ?


Log in to reply