Results 1 to 10 of 10

Thread: why do buttons don't change color?

  1. #1
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Unhappy why do buttons don't change color?

    Hi everyone.

    I'm using Qt4 on Windows and I'm encountering troubles with the buttons colors.
    Two questions I have:
    1) Even changing the QPalette::Button and QPalette::ButtonText colors of a QPushButton palette, why just the text gets changed and not the background? Do palettes support multiple changes? Here follows the piece of code which changes the palette:
    Qt Code:
    1. pal.setColor(QPalette::ButtonText, Qt::white);
    2. pal.setColor(QPalette::Button, Qt::black);
    3. ui.cmdsounddod->setPalette(pal);
    To copy to clipboard, switch view to plain text mode 

    2) To Qt developers: was this palette approach needed?
    Well, I mean, in Qt3 to change a color needed a call to
    Qt Code:
    1. uiobject->setBackgroundColor(QColor)
    To copy to clipboard, switch view to plain text mode 
    and now it needs to pass through the palette that I find a bit complex...
    I wouldn't be surprised if I miss a quicker way to change a ui object color...

    Thank you in advance,
    any help is appreciated
    Last edited by Raccoon29; 11th November 2007 at 23:04. Reason: spelling error
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: why do buttons don't change color?

    The problem is that different platforms interpret the Palettes differently. Which color role is used for what is decided by the QStyle. On Linux the Plastique style uses the Button Palette for the color of a button. The Windows style however uses the BaseRole for that purpose. This inconsistent usage of palettes has always been an issue that led to the creation of Qt StyleSheets.

    StyleSheets are much simpler and you have guaranties that a color will be like that on every platform (expect MacOSX as of now). The need for having a red button on every platform, was a particular problem that led to the creation of Stylesheets in the first place.
    You may want to read this about the "Red Pushbutton project": http://chaos.troll.no/~ahanssen/devdays2007/StyleSheets.pdf


    As of changing the background-color of your button ... try this out:
    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. int main(int argc, char* argv[])
    5. {
    6. QApplication app(argc, argv);
    7. QPushButton* btn = new QPushButton("Press me");
    8. btn->setStyleSheet(
    9. "background:qlineargradient(x1:0, y1:0, x2:0, y2:1,"
    10. "stop: 0 black, stop: 0.4 gray, stop: 0.5 darkgray, stop: 1.0 black);"
    11. "color:yellow; font-size:24px;"
    12. "font-weight:bold; border-radius:10%;"
    13. "margin:10px; border:3px dashed green;"
    14. );
    15. btn->show();
    16. btn->resize(200,80);
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 
    This should yield a result like this:
    button.jpg

    Now tell me, would it have been that easy with Qt3?
    Read this for more information: http://doc.trolltech.com/4.3/stylesh...ng-qpushbutton
    Last edited by momesana; 12th November 2007 at 00:34.

  3. The following 2 users say thank you to momesana for this useful post:

    Raccoon29 (13th November 2007), tommy (13th November 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: why do buttons don't change color?

    Quote Originally Posted by Raccoon29 View Post
    2) To Qt developers: was this palette approach needed?
    Well, I mean, in Qt3 to change a color needed a call to
    Qt Code:
    1. uiobject->setBackgroundColor(QColor)
    To copy to clipboard, switch view to plain text mode 
    and now it needs to pass through the palette that I find a bit complex...
    I wouldn't be surprised if I miss a quicker way to change a ui object color...
    I'm not Qt developer, but I think I might try to answer that, at least until more appropriate people decide to reply as well.

    In Qt3 you had a palette object as well (QColorGroup). There just was a method to quick access the foreground and background component of it.

  5. #4
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: why do buttons don't change color?

    Now tell me, would it have been that easy with Qt3?
    Oh my! This stylesheet is amazing! Right like a powerfull CSS style!
    Well... I knew it... there was a quicker way that I didn't know
    This Qt4 seems to be way more mighty than Qt3!

    Thank you for all explanations!
    Thank you, thank you, thank you!
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  6. #5
    Join Date
    Sep 2007
    Posts
    18
    Thanks
    3

    Default Re: why do buttons don't change color?

    BTW, Using stylesheets in place of
    setBackgroundColor()... is not too bad,
    but my doubt is,
    while I am interested in ONLY the background of the widget, WHY should I worry about its margin, border, .... also why should change the look of the widget when it is pressed
    QPushButtonressed(...)??

    And to change the background color, they use qlineargradient, I want to set the SPREAD property of the same in the stylesheets. How to I achieve this?

    In fact, changing the backcolor through stylesheets is easy but not for all widgets, like QPushButton..

  7. #6
    Join Date
    Sep 2007
    Location
    Szczecin, Poland
    Posts
    153
    Thanks
    7
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: why do buttons don't change color?

    Quote Originally Posted by wysota View Post
    There just was a method to quick access the foreground and background component of it.
    Qt4 still have those methods to access to foreground & background color:
    Qt Code:
    1. void QWidget::setBackgroundRole ( QPalette::ColorRole role );
    2. void QWidget::setForegroundRole ( QPalette::ColorRole role );
    To copy to clipboard, switch view to plain text mode 
    but, there's one method more, that often causes background color isn't applied:
    Qt Code:
    1. void setAutoFillBackground ( bool enabled );
    To copy to clipboard, switch view to plain text mode 
    This property holds whether the widget background is filled automatically.
    If enabled, this will cause Qt to fill the background using the widget's background role before invoking the paint event. The background role is defined by the widget's palette.
    In addition, Windows are always filled with QPalette::Window, unless the WA_OpaquePaintEvent or WA_NoSystemBackground attributes are set.
    Warning: Do not set this property when using Qt Style Sheets. When using style sheets, the widget is always filled with the "background" property. The "border-radius" property may be used to clip the background.
    See, qt assumes that custom backgrounds are rare cases, so to optimize painting they don't draw it at all by default, If you're setting custom background role, you shall probably turn autofillbackground on - I'm nearly sure that's your problems source.
    See GrEEn (Graphics Effects Environment)
    http://sourceforge.net/project/platf...roup_id=232746
    a qt-based plugins oriented MDI image processing application(contains also qt plugins like styles & imageformats).

  8. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: why do buttons don't change color?

    Quote Originally Posted by dyams View Post
    BTW, Using stylesheets in place of
    setBackgroundColor()... is not too bad,
    but my doubt is,
    while I am interested in ONLY the background of the widget, WHY should I worry about its
    You only need to set the properties you are interested it. The example above was a little verbose because I wanted to highlight the possibilities that Qt4 in conjunction with Stylesheets has to offer.

    Quote Originally Posted by dyams View Post
    margin, border, .... also why should change the look of the widget when it is pressed
    QPushButtonressed(...)??
    You don't have to but then the pressed stage wouldn't look different from the normal state. And you can not except Qt to calculate the highlighted state for a button after custom-painting its background.

    Quote Originally Posted by dyams View Post
    And to change the background color, they use qlineargradient, I want to set the SPREAD property of the same in the stylesheets. How to I achieve this?
    You can do all the gradient effects offered to you by the QGradient derived classes. Read the respective docs and the examples provided by Qt.


    Quote Originally Posted by dyams View Post
    In fact, changing the backcolor through stylesheets is easy but not for all widgets, like QPushButton..
    It's actually almost always the easier solution. And it provides the possibility to make an application themable in a very simple manner. And you can set the properties selectively. For example:

    Qt Code:
    1. QPushButton#OkButton { background-color:yellow; }
    To copy to clipboard, switch view to plain text mode 
    would *only* change the background color of Buttons that have "OkButton" set as their object names. Applying stylesheets to the QApplication (qApp->setStyleSheets()) makes the whole application apply the style. just provide a bunch of css files and facilities to load them and you have a stylable program. I think that's pretty amazing.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: why do buttons don't change color?

    Quote Originally Posted by dyams View Post
    In fact, changing the backcolor through stylesheets is easy but not for all widgets, like QPushButton..
    Actually this is currently the only possibility to change the background colour of a button in a reliable way. Other solutions (like changing the palette) simply won't work on WinXP, Vista or Aqua.

  10. #9
    Join Date
    Sep 2007
    Location
    Szczecin, Poland
    Posts
    153
    Thanks
    7
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: why do buttons don't change color?

    Quote Originally Posted by wysota View Post
    Other solutions (like changing the palette) simply won't work on WinXP, Vista or Aqua.
    it looks like some styles are using fixed colors, so maybe it's good idea to inherit those styles & fix the problem there once instead of using styleSheets for every single button?
    See GrEEn (Graphics Effects Environment)
    http://sourceforge.net/project/platf...roup_id=232746
    a qt-based plugins oriented MDI image processing application(contains also qt plugins like styles & imageformats).

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: why do buttons don't change color?

    Quote Originally Posted by mchara View Post
    it looks like some styles are using fixed colors, so maybe it's good idea to inherit those styles & fix the problem there once
    No. XP, Vista and Aqua use their native routines to paint the gui and they don't allow changing colours of buttons. The way stylesheets work is that they provide an additional style layer and either do the drawing themselves or pass the call to the original style, depending on what the style is and what the stylesheet contains.

    instead of using styleSheets for every single button?
    You can apply a stylesheet on the application object and thus change all buttons with a single call.

Similar Threads

  1. Change background color of QPushButton
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2013, 10:23
  2. Change QPushButton Background Color, Qt4.3
    By Rayven in forum Qt Programming
    Replies: 5
    Last Post: 4th July 2009, 07:14
  3. statusBar() message color change
    By mclark in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2007, 23:20
  4. Replies: 2
    Last Post: 9th March 2007, 23:19
  5. [QTable] Change row's color
    By villy in forum Qt Programming
    Replies: 5
    Last Post: 15th November 2006, 19:21

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.