Results 1 to 7 of 7

Thread: How to change Pushbutton color with QPalette

  1. #1
    Join Date
    Sep 2017
    Posts
    29
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default How to change Pushbutton color with QPalette

    I tried:

    palOn.setColor(QPalette::Button, Qt::green);
    btn->setPalette(palOn);

    doesn't work. Only work when i select Flat on QtDesigner. But i dont want flat.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to change Pushbutton color with QPalette

    This is almost impossible to do on Windows. No matter what you try, either style sheet or palette, the button drawing code does its own thing. The most common solution is to derive a new custom class (*ahem*) from QPushButton and override the paintEvent:

    Qt Code:
    1. // ColorButton.h
    2.  
    3. #pragma once
    4.  
    5. #include <QPushButton>
    6.  
    7. class ColorButton : public QPushButton
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. ColorButton( const QString & text, QWidget * pParent = 0 );
    13. virtual ~ColorButton();
    14.  
    15. void setBackgroundColor( const QColor & color );
    16.  
    17. protected:
    18. void paintEvent( QPaintEvent * pEvent );
    19.  
    20. protected:
    21. QColor background;
    22. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // ColorButton.cpp
    2.  
    3. #include "ColorButton.h"
    4. #include <QStylePainter>
    5. #include <QStyleOptionButton>
    6. #include <QPaintEvent>
    7.  
    8. ColorButton::ColorButton( const QString & text, QWidget * pParent )
    9. : QPushButton( text, pParent )
    10. , background( Qt::red )
    11. {
    12. }
    13.  
    14. ColorButton::~ColorButton()
    15. {
    16. }
    17.  
    18. void ColorButton::setBackgroundColor( const QColor & color )
    19. {
    20. background = color;
    21. update();
    22. }
    23.  
    24. void ColorButton::paintEvent( QPaintEvent * pEvent )
    25. {
    26. QStylePainter painter( this );
    27. painter.fillRect( pEvent->rect(), background );
    28.  
    29. initStyleOption( &option );
    30.  
    31. if ( !isFlat() )
    32. painter.drawPrimitive( QStyle::PE_FrameButtonBevel, option );
    33.  
    34. if ( isDefault() )
    35. painter.drawPrimitive( QStyle::PE_FrameDefaultButton, option );
    36.  
    37. if ( hasFocus() )
    38. painter.drawPrimitive( QStyle::PE_FrameFocusRect, option );
    39.  
    40. painter.drawControl( QStyle::CE_PushButtonLabel, option );
    41.  
    42. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // main.cpp test program
    2.  
    3. #include <QWidget>
    4. #include <QApplication>
    5. #include <QVBoxLayout>
    6. #include "ColorButton.h"
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app( argc, argv );
    11.  
    12. QWidget window ;
    13.  
    14. window.setWindowTitle( QString::fromUtf8( "QPushbutton Background" ) );
    15. window.resize( 336, 227 );
    16.  
    17. ColorButton *button1 = new ColorButton( "Button 1" );
    18. ColorButton *button2 = new ColorButton( "Button 2" );
    19. ColorButton *button3 = new ColorButton( "Button 3" );
    20.  
    21. QVBoxLayout* layout = new QVBoxLayout( &window );
    22. layout->addWidget( button1 );
    23. layout->addWidget( button2 );
    24. layout->addWidget( button3 );
    25.  
    26.  
    27. button1->setBackgroundColor( Qt::lightGray );
    28. button2->setBackgroundColor( Qt::green );
    29. button3->setBackgroundColor( Qt::transparent );
    30.  
    31. button1->setCheckable( true );
    32. button1->setChecked( true );
    33.  
    34. button2->setEnabled( false );
    35. button3->setDefault( true );
    36.  
    37. window.show();
    38. return app.exec();
    39.  
    40. }
    To copy to clipboard, switch view to plain text mode 

    This is a simplistic override of QPushButton. It implements only a few of the painting styles that change with the button state. Some of them are automatically done by the primitives (like drawing the checked state or the border of the default button). Button2 is disabled, but the only obvious thing is that the text is grayed out.

    Other things probably need to be implemented (like drawing the icon if the button has one). Doing that is left as an exercise for the reader.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Sep 2017
    Posts
    29
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: How to change Pushbutton color with QPalette

    It's okay I will find a book and learn it.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to change Pushbutton color with QPalette

    Pretty hard to make you happy, isn't it? Took me about 3 hours to figure out the answer to this one when the simple QPalette-based solution didn't work for me either.

    Enjoy your books. I don't think I'll bother to answer any of your future questions.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to change Pushbutton color with QPalette

    This is almost impossible to do on Windows. No matter what you try, either style sheet or palette, the button drawing code does its own thing.
    Hmm, interesting.
    I remember how back in the day, this was an issue of its own "the red button issue".
    I remember how either the release of Qt3 or Qt4 (can't remember which anymore) during dev days they actually used it as a show case of how the "problem of the red button" is so simple to implement with Qt (might have been the introduction of QStyleSheet, but I am not sure.
    If this is true (which I believe it is after you have tried it) then its a regression.
    Last edited by high_flyer; 21st September 2017 at 10:43.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to change Pushbutton color with QPalette

    If this is true (which I believe it is after you have tried it) then its a regression.
    I believe it might have occurred with the transition to the new Qt5 implementation of platform-specific code in a driver library (the qwindows.dll in /platforms) and the refactoring that was needed for that.

    Even in the code I wrote above, it was very difficult to get the panel of the button to be red. Any call to QStylePainter::drawControl() with a CE_PushButton* argument other than "Label" results in a button with a red border and the panel filled in by the system color. And even with the code I wrote, the button appearance and behavior is subtly different from what you get from a standard QPushButton. I think it would take a lot more work to get a button that looked and acted the same but with a color of your own choosing.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Sep 2017
    Posts
    29
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: How to change Pushbutton color with QPalette

    Quote Originally Posted by d_stranz View Post
    Pretty hard to make you happy, isn't it? Took me about 3 hours to figure out the answer to this one when the simple QPalette-based solution didn't work for me either.

    Enjoy your books. I don't think I'll bother to answer any of your future questions.
    Aww... sorry man. I appreciate your coding and subclass example.
    Thank you.

  8. The following user says thank you to GeneCode for this useful post:

    d_stranz (25th September 2017)

Similar Threads

  1. Replies: 6
    Last Post: 16th April 2013, 08:30
  2. QGtkStyle - Ignores QPalette color overrides
    By chezifresh in forum Qt Programming
    Replies: 1
    Last Post: 9th July 2009, 01:49
  3. QPalette on PushButton
    By wagmare in forum Qt Programming
    Replies: 3
    Last Post: 30th January 2009, 13:44
  4. QPalette change signal
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 19th September 2008, 15:02
  5. QPalette won't set QLabel's Window & Foreground color
    By koklim in forum Qt Programming
    Replies: 6
    Last Post: 23rd January 2006, 10:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.