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. Work around for changing tool button's background color
Forum Updated to NodeBB v4.3 + New Features

Work around for changing tool button's background color

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 3 Posters 6.2k 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.
  • O Offline
    O Offline
    Omni_Philm
    wrote on 27 Jun 2019, 21:01 last edited by
    #1

    Hello all,

    I was wondering if there are any good work around for changing the background color of a QToolButton?

    Currently, I am using the style sheet to change the background. However, I need to remove the border to have the color displayed. But doing that, the button no longer looks like a button.

    I was thinking that I could make the background transparent and have a color label behind the button that changes colors. Not sure if this is the best approach but I wanted to see if any has any other better ideas?

    Thank you

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 28 Jun 2019, 05:04 last edited by
      #2

      You can change background color in QPalette, then apply it to your tool button. So something like:

      QToolButton *button = ...
      QPalette palette = button.palette();
      palette.setBrush(QPalette::Base, QColor(Qt::red));
      palette.setBrush(QPalette::Button, QColor(Qt::red));
      button.setPalette(palette);
      

      (Z(:^

      1 Reply Last reply
      1
      • O Offline
        O Offline
        Omni_Philm
        wrote on 28 Jun 2019, 20:47 last edited by
        #3

        @sierdzio

        Thank you for the tip. I have tried various combinations of these:

          p_deactiveColor.setBrush(QPalette::Base, QColor(Qt::red));
          p_deactiveColor.setBrush(QPalette::Button, QColor(Qt::red));
          p_deactiveColor.setBrush(QPalette::Window, QColor(Qt::red));
          p_deactiveColor.setBrush(QPalette::WindowText, QColor(Qt::red));
        
            ui->cupExtendButton->setPalette(p_deactiveColor);
        

        And on the form only the right and left edges are being colored red. Is there another property that I need to set?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 28 Jun 2019, 22:30 last edited by
          #4

          Hi
          You can just give it border back but i suspect its something else you mean
          since using a stylesheet did not do what you wanted?
          alt text

          1 Reply Last reply
          1
          • O Offline
            O Offline
            Omni_Philm
            wrote on 1 Jul 2019, 19:10 last edited by
            #5

            @mrjj Do you know what the CSS Styling is for the default border? I would like to keep everything about the button the same except for changing the background color

            M 1 Reply Last reply 1 Jul 2019, 21:14
            0
            • O Omni_Philm
              1 Jul 2019, 19:10

              @mrjj Do you know what the CSS Styling is for the default border? I would like to keep everything about the button the same except for changing the background color

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 1 Jul 2019, 21:14 last edited by
              #6

              @Omni_Philm
              Hi
              Sadly its not a stylesheet for the original border.
              Its draw by the QStyle for the used platform.

              The paint event looks like this

              void QToolButton::paintEvent(QPaintEvent *)
              {
                  QStylePainter p(this);
                  QStyleOptionToolButton opt;
                  initStyleOption(&opt);
                  p.drawComplexControl(QStyle::CC_ToolButton, opt);
              }
              
              

              But on windows os, it seems to ignore any palette change and i think its not using it for the background.
              At least i could not make it change.

              If you can live with text also getting colored, you could do

              #include <QToolButton>
              #include <QStyleOptionToolButton>
              #include <QStylePainter>
              
              class ColorButton : public QToolButton
              {
                  Q_OBJECT
              public:
                  explicit ColorButton(QWidget *parent = nullptr) : QToolButton(parent) {}
              protected:
                  virtual void paintEvent(QPaintEvent *) override
                  {
                      QStylePainter p(this);
                      QStyleOptionToolButton opt;
                      initStyleOption(&opt);        
                      p.drawComplexControl(QStyle::CC_ToolButton,   opt);
                      p.setBrush(QColor(255,0,0,100));
                      p.setPen(Qt::NoPen);
                      p.drawRect(4,4,width()-8,height()-8);
                  }
              };
              

              First draw the button normally and then put transparent rect on top.

              result:
              alt text

              1 Reply Last reply
              1
              • O Offline
                O Offline
                Omni_Philm
                wrote on 3 Jul 2019, 17:38 last edited by
                #7

                @mrjj Thanks for the tip.

                I am not so sure about subclassing. I have a number of buttons that need this. I already am subclassing the lineEdit button to create a clickable signal. And replacing that with the one that I place in the designer can get a little hairy.

                Anyways, I wouldn't mind the rectangle idea but the buttons are within a layout. Isn't the positional coordinates referenced to the layout?

                I am surprised that this issue exists? Would this be a "bug" within the library itself or could I be doing something incorrectly? I think that I am going to create a simple test program to demonstrate what is happening. I feel that the QPalette code should be working correctly?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 3 Jul 2019, 19:36 last edited by mrjj 7 Mar 2019, 19:38
                  #8

                  @Omni_Philm said in Work around for changing tool button's background color:

                  And replacing that with the one that I place in the designer can get a little hairy.
                  Actually, if you use the promotion feature its pretty easy if you use UI files.
                  https://doc.qt.io/qt-5/designer-using-custom-widgets.html

                  -Isn't the positional coordinates referenced to the layout?

                  Nope its drawn on top so its 100% fixed to the button so layouts etc is not an issue.

                  -I feel that the QPalette code should be working correctly?
                  Well its not all QStyles that use the palette on all platforms.

                  1 Reply Last reply
                  0
                  • O Offline
                    O Offline
                    Omni_Philm
                    wrote on 3 Jul 2019, 19:50 last edited by
                    #9

                    @mrjj

                    Lets explore the rectangle option, how do I fix it to a particular button?

                    M 1 Reply Last reply 4 Jul 2019, 06:17
                    0
                    • O Omni_Philm
                      3 Jul 2019, 19:50

                      @mrjj

                      Lets explore the rectangle option, how do I fix it to a particular button?

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 4 Jul 2019, 06:17 last edited by
                      #10

                      @Omni_Philm
                      hi
                      its not fixed to it. its painted on top.

                      1 Reply Last reply
                      0
                      • O Offline
                        O Offline
                        Omni_Philm
                        wrote on 10 Jul 2019, 18:56 last edited by
                        #11

                        @mrjj said in Work around for changing tool button's background color:

                        Q_OBJECT
                        public:
                        explicit ColorButton(QWidget *parent = nullptr) : QToolButton(parent) {}
                        protected:
                        virtual void paintEvent(QPaintEvent *) override
                        {
                        QStylePainter p(this);
                        QStyleOptionToolButton opt;
                        initStyleOption(&opt);
                        p.drawComplexControl(QStyle::CC_ToolButton, opt);
                        p.setBrush(QColor(255,0,0,100));
                        p.setPen(Qt::NoPen);
                        p.drawRect(4,4,width()-8,height()-8);
                        }

                        @mrjj

                        So I finally had a chance to try out your code. I think that it holds promise but the text will need to be black. Is there any way that I can force draw the text on foreground for everything?

                        Also, the pic you have in shows that the text is not really colored with red. However, for me, the text is colored with red but the black is seeping through?

                        1 Reply Last reply
                        0
                        • O Offline
                          O Offline
                          Omni_Philm
                          wrote on 18 Jul 2019, 15:37 last edited by
                          #12

                          Any thoughts?

                          M 1 Reply Last reply 18 Jul 2019, 15:49
                          0
                          • O Omni_Philm
                            18 Jul 2019, 15:37

                            Any thoughts?

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 18 Jul 2019, 15:49 last edited by
                            #13

                            @Omni_Philm
                            Hi
                            The overlay is transparent so it only color the black text a bit.
                            The (QColor(255,0,0,100)); , the last 100 is how trans it is.

                            Well you could remove the text from opt. (set to QString() )
                            so there is no text for
                            p.drawComplexControl(QStyle::CC_ToolButton, opt);
                            to draw
                            and then use p.drawText
                            after
                            p.drawRect
                            to have text on top.
                            However, if u use any text-align etc on the button, those will break unless you handle that too for the
                            custom drawing.

                            1 Reply Last reply
                            0
                            • O Offline
                              O Offline
                              Omni_Philm
                              wrote on 22 Jul 2019, 20:12 last edited by
                              #14

                              @mrjj

                              Hello, I am trying to use the drawText function. Here is what I have so far:

                              p.setPen(QColor(0,0,0));
                              p.setFont(this->font());
                              p.drawText(this->frameGeometry(), Qt::AlignCenter, tempText);
                              

                              What happens is that no text is actually drawn. I figured I would use a rectangle on the QToolButton to bind the text to. That way, I can preserve my alignment.

                              M 1 Reply Last reply 23 Jul 2019, 07:15
                              0
                              • O Omni_Philm
                                22 Jul 2019, 20:12

                                @mrjj

                                Hello, I am trying to use the drawText function. Here is what I have so far:

                                p.setPen(QColor(0,0,0));
                                p.setFont(this->font());
                                p.drawText(this->frameGeometry(), Qt::AlignCenter, tempText);
                                

                                What happens is that no text is actually drawn. I figured I would use a rectangle on the QToolButton to bind the text to. That way, I can preserve my alignment.

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 23 Jul 2019, 07:15 last edited by
                                #15

                                @Omni_Philm
                                what do u set tempText too ?

                                O 1 Reply Last reply 23 Jul 2019, 15:32
                                0
                                • M mrjj
                                  23 Jul 2019, 07:15

                                  @Omni_Philm
                                  what do u set tempText too ?

                                  O Offline
                                  O Offline
                                  Omni_Philm
                                  wrote on 23 Jul 2019, 15:32 last edited by Omni_Philm
                                  #16

                                  @mrjj

                                  Oops, forgot to include that line in the example:

                                  but

                                  tempText = opt.text;
                                  

                                  Edit:

                                  here is the full drawing code:

                                          QString tempText;
                                          QStylePainter p(this);
                                          QStyleOptionToolButton opt;
                                          initStyleOption(&opt);
                                          tempText = opt.text;
                                          opt.text = QString();
                                          p.save();
                                          p.drawComplexControl(QStyle::CC_ToolButton, opt);
                                          p.setBrush(QColor(255,0,0,100));
                                          p.setPen(Qt::NoPen);
                                          p.drawRect(4,4,width()-8,height()-8);
                                  
                                       //   p.setBrush(QColor(0,0,0));
                                          p.setPen(QColor(0,0,0));
                                  
                                          p.setFont(this->font());
                                  
                                          p.drawText(this->frameGeometry(), Qt::AlignCenter, tempText);
                                          p.restore();
                                  
                                  1 Reply Last reply
                                  0
                                  • O Offline
                                    O Offline
                                    Omni_Philm
                                    wrote on 24 Jul 2019, 15:33 last edited by
                                    #17

                                    Any thoughts?

                                    1 Reply Last reply
                                    0
                                    • O Offline
                                      O Offline
                                      Omni_Philm
                                      wrote on 26 Jul 2019, 19:46 last edited by
                                      #18

                                      Ok, I got, had to change one of the lines of code to:

                                      p.drawText(this->contentsRect(), Qt::AlignCenter, tempText);

                                      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