Page 1 of 2 12 LastLast
Results 1 to 20 of 34

Thread: Colored Buttons

  1. #1
    Join Date
    Mar 2007
    Location
    Vienna / Austria
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Colored Buttons

    How can I give a Button another BackgroundColor??

    I tried it with:

    Qt Code:
    1. myButton->setPalette(QPalette(QColor(Qt::green)));
    To copy to clipboard, switch view to plain text mode 

    But it doesn't work; I got following error at compiling the c-file:

    Qt Code:
    1. error: expected primary-expression before '(' token.
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    Qt Code:
    1. QPalette palette;
    2. palette->setBrush( QPalette::Background, QBrush( Qt::green ) );
    3. myButton->setPalette( palette );
    To copy to clipboard, switch view to plain text mode 

    Check out the color roles in QPalette.

    Regards

  3. The following user says thank you to marcel for this useful post:

    Walsi (24th April 2007)

  4. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    Actually, you may have to use the role QPalette::Window, to change the button color.

  5. #4
    Join Date
    Mar 2007
    Location
    Vienna / Austria
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    Quote Originally Posted by marcel View Post
    Actually, you may have to use the role QPalette::Window, to change the button color.
    Please explain more detailed!

  6. #5
    Join Date
    Mar 2007
    Location
    Vienna / Austria
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    Quote Originally Posted by marcel View Post
    Qt Code:
    1. QPalette palette;
    2. palette->setBrush( QPalette::Background, QBrush( Qt::green ) );
    3. myButton->setPalette( palette );
    To copy to clipboard, switch view to plain text mode 

    Check out the color roles in QPalette.

    Regards
    It doesn't work so, I got following error at compiling the c-fle:

    Qt Code:
    1. error: base operand of '->' has non-pointer type 'QPalette'
    To copy to clipboard, switch view to plain text mode 

    I got no errors when I replace '->' with '.'

    Look:

    Qt Code:
    1. QPalette greenPalette;
    2. greenPalette.setBrush(QPalette::Background, QBrush(Qt::green));
    3. destroyButton->setPalette(greenPalette);
    To copy to clipboard, switch view to plain text mode 

    But the Button is still gray?

  7. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    Use QPalette::Window, instead of Background.

  8. #7
    Join Date
    Mar 2007
    Location
    Vienna / Austria
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    Button is still gray!


  9. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    palette->setColor( QPalette::Window, Qt::green );

    Regards

  10. The following user says thank you to marcel for this useful post:

    Walsi (24th April 2007)

  11. #9
    Join Date
    Mar 2007
    Location
    Vienna / Austria
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    "." instead of "->"

    because with the "->" I got a compiling error!

  12. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    Sure, I forgot you don't have a palette pointer

  13. #11
    Join Date
    Mar 2007
    Location
    Vienna / Austria
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    but the button is still gray!


  14. #12
    Join Date
    Mar 2007
    Location
    Vienna / Austria
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    arrrr it is green!!!

    I even dont know what my mistake was!!

    How can i use colors which are net predefined??

    Is it possible to enter anywhere a RGB code???

    Or something else?

    Best Regards, and thx

  15. #13
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    Quote Originally Posted by marcel View Post
    Actually, you may have to use the role QPalette::Window, to change the button color.
    i'm having trouble changing the background color of the pushbutton, regardless of the chosen role. The only way i've been able to change it is by manipulating the styleSheet. Could you perhaps post some compilable example code where you alter the button's background color?

    Edit: I take that back, using QPalette::Button worked like a charm.
    Qt Code:
    1. QPalette palette = button->palette();
    2. palette.setColor(QPalette::Button, Qt::red);
    3. button->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 
    Last edited by TheRonin; 24th April 2007 at 08:37. Reason: evreka!

  16. #14
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    Yes, you can construct a custom color with QColor( R, G, B ).
    Look at QColor.

    Regards

  17. #15
    Join Date
    Mar 2007
    Location
    Vienna / Austria
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    Quote Originally Posted by TheRonin View Post
    i'm having trouble changing the background color of the pushbutton, regardless of the chosen role. The only way i've been able to change it is by manipulating the styleSheet. Could you perhaps post some compilable example code where you alter the button's background color?

    That are the lines I inserted to change the butons color:

    Qt Code:
    1. #include <QPalette>
    2. QPalette greenPalette;
    3. greenPalette.setColor(QPalette::Window, Qt::green);
    4. destroyButton->setPalette(QPalette(QColor(Qt::green)));
    To copy to clipboard, switch view to plain text mode 

    Don't ask about the name of the Button

    Best Regards,

  18. #16
    Join Date
    Mar 2007
    Location
    Vienna / Austria
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    Quote Originally Posted by marcel View Post
    Yes, you can construct a custom color with QColor( R, G, B ).
    Look at QColor.

    Regards
    Another basic question from me: Is a QPushButton able to blink??

    For example: gray --> green --> gray --> green

    Best Regards!

  19. #17
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    Yes, if you use a QTimer, with let's say a 200ms delay. In the timerEvent slot you just switch the color from gray to green and then back.

    Regards

  20. #18
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Colored Buttons

    A few notes:

    It is a good practice to get the palette from a widget, modify it, and set it back. Constructing a new palette object makes it use application's default palette which means losing any possible previous modifications to widget's palette:
    Qt Code:
    1. QPalette palette = widget->palette();
    2. palette.setColor(...);
    3. widget->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 

    For a button, the correct color group is QPalette::Button:
    Qt Code:
    1. QPalette palette = button->palette();
    2. palette.setColor(QPalette::Button, Qt::green);
    3. button->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 

    QWindowsXpStyle and QMacStyle use native theming engines which cause some palette modifications not to have any effect. Certain colors/brushes/whatever come from the underlying system. Style sheets is the solution to this problem. Follow the link and see the example of changing QPushButton's color there.
    J-P Nurmi

  21. #19
    Join Date
    Mar 2007
    Location
    Vienna / Austria
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    I got a runtime exception: Segmentation fault!

  22. #20
    Join Date
    Mar 2007
    Location
    Vienna / Austria
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored Buttons

    Now I tried a solution which causes no error and which seems to be working.

    My code is looking like that:

    I generate my own color out from a RGB code (also with alpha Parameter)

    Qt Code:
    1. QColor myColor;
    2. mycolor.setRgb(0, 150, 0, 255);
    3.  
    4. ...
    5. ...
    6. ...
    7.  
    8. myButton->setPalette(QPalette(Qcolor(myColor)));
    To copy to clipboard, switch view to plain text mode 


    It works fine!

    Best Regards,...

  23. The following user says thank you to Walsi for this useful post:

    fnmblot (25th April 2007)

Similar Threads

  1. Main window with custom buttons ?
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2006, 06:32
  2. array of radio buttons
    By amulya in forum Qt Programming
    Replies: 4
    Last Post: 5th October 2006, 12:59
  3. widget for text AND buttons
    By nongentesimus in forum Newbie
    Replies: 2
    Last Post: 16th June 2006, 13:43
  4. Replies: 9
    Last Post: 9th May 2006, 19:53
  5. How to get larger radio buttons on XP?
    By Ben.Hines in forum Qt Programming
    Replies: 9
    Last Post: 24th April 2006, 19:00

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
  •  
Qt is a trademark of The Qt Company.