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. Don't get custom QStyle to do any effect
Forum Updated to NodeBB v4.3 + New Features

Don't get custom QStyle to do any effect

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 382 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.
  • G Offline
    G Offline
    GGaston
    wrote on last edited by
    #1

    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);
      }
    }
    
    G 1 Reply Last reply
    0
    • G GGaston

      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);
        }
      }
      
      G Offline
      G Offline
      GGaston
      wrote on last edited by
      #2

      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 &).

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        Can you show your style code ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        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