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. QPushButton Overlay Color
Qt 6.11 is out! See what's new in the release blog

QPushButton Overlay Color

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.9k 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.
  • E Offline
    E Offline
    exiled
    wrote on last edited by
    #1

    Well I basically was to make a QPushButton which has a color contained with in it, basically a box with a color overlapped on top of a button. When you press the button it'll open a QColorDialog which you than pick a color, setting the color overlapped on top of the button to the newly selected color. I'd prefer to not make a new control so I was wondering if there is a way to do this.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p-himik
      wrote on last edited by
      #2

      The easiest way to do it is to reimplement paintEvent().
      Something like (not tested)
      @void ColorButton::paintEvent( QPaintEvent* event )
      {
      QPainter painter( this );
      painter.setBrush( QBrush( currentColor_ ) );
      painter.drawRect( event->rect() );
      }@

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        In your reimplementation, you forgot to include a call to the base class implementation. The code above will only draw the square.

        @

        void ColorButton::paintEvent( QPaintEvent* event )
        {
        QPushButton::paintEvent(event); // <-- do not forget this line

        QPainter painter( this );
        painter.setBrush( QBrush( currentColor_ ) );
        painter.drawRect( event->rect() );
        

        }
        @

        You might also considder just creating a pixmap, and setting that on the button with setPixmap if the color changes. You can do that either in a subclass or separately from the button code.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p-himik
          wrote on last edited by
          #4

          Andre, that was intentionally. Both of yours and mine functions will draw only a rectangle (painter.drawRect( event->rect() ) completely paints out base button) so there is no need to call base class implementation.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Doh, you are right of course.

            Then again, I would not draw the rectangle over the whole button, normally. But indeed, my version too will yield the same result at lower performance than yours.

            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