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. Changing QPalette of widget inside QGraphicsScene not working
Qt 6.11 is out! See what's new in the release blog

Changing QPalette of widget inside QGraphicsScene not working

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.5k Views 2 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.
  • V Offline
    V Offline
    vinci
    wrote on last edited by
    #1

    Hello

    I'm trying to change the color of a widget which inherits from QFrame using QPalette. This widget is then used in a QGraphicsScene.

    class State : public QFrame {
    public:
      State(QString name, QWidget* parent = nullptr) : QFrame{parent} {
        QVBoxLayout* layout{new QVBoxLayout};
        label_->setText(name);
        layout->addWidget(label_);
        setLayout(layout);
        setFrameStyle(QFrame::Box);
    
        // Not working at all
        QPalette palette{QFrame::palette()};
        palette.setColor(QPalette::Base, Qt::red);
        setPalette(palette);
      }
    
    private:
      QLabel* label_{new QLabel};
    };
    

    Sadly this doesn't seem to do anything? I've tried changing various ColorRoles (e.g. Window) but the widget still looks the same. I'm currently using Qt 6.3.0.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vinci
      wrote on last edited by
      #2

      I just realized that it actually does work, but only if I disable my stylesheet.
      Is this behavior wanted? Does a stylesheet take precedence over locally set palettes?

      JonBJ 1 Reply Last reply
      0
      • V vinci

        I just realized that it actually does work, but only if I disable my stylesheet.
        Is this behavior wanted? Does a stylesheet take precedence over locally set palettes?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @vinci said in Changing QPalette of widget inside QGraphicsScene not working:

        Does a stylesheet take precedence over locally set palettes?

        Yep.

        1 Reply Last reply
        2
        • V Offline
          V Offline
          vinci
          wrote on last edited by
          #4

          Oh I see thank you.

          What's the recommended best practice to "slightly" change a widgets style (e.g. border color) then in case I can't change the applied stylesheet? I really only need some kind of visual cue for two different kind of states.

          S 1 Reply Last reply
          0
          • V vinci

            Oh I see thank you.

            What's the recommended best practice to "slightly" change a widgets style (e.g. border color) then in case I can't change the applied stylesheet? I really only need some kind of visual cue for two different kind of states.

            S Offline
            S Offline
            SimonSchroeder
            wrote on last edited by
            #5

            @vinci QPalette is really unreliable. Some colors cannot be changed by the QPalette and it depends on the style which palette colors influence which part of the widgets. So, I advocate to try to not use QPalette at all.

            The best way is actually using stylesheets. You can just set a stylesheet on just this one QFrame object. It can be a small stylesheet changing only this one little thing. Everything else will be inherited from your current global stylesheet.

            1 Reply Last reply
            2
            • V Offline
              V Offline
              vinci
              wrote on last edited by
              #6

              Alright, I'll look into that.
              Thanks for the fast answers!

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

                Hi,

                I would recommend a look at this excellent article from KDAB about using custom QStyle.

                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