Results 1 to 7 of 7

Thread: Get background color of QPushbutton

  1. #1
    Join Date
    Jan 2014
    Posts
    16
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Question Get background color of QPushbutton

    Hi,all.

    I have designed some QPushbutton controls using QT designer ,and set background color of the push-buttons to some color. Just like:
    TestColor.JPG, Now i only can get Qstring about background color by code:
    Qt Code:
    1. QString str=ui->btn_curveColor->styleSheet();
    2. cout<<str.toAscii().data()<<endl;
    To copy to clipboard, switch view to plain text mode 
    .
    it gives me :

    Qt Code:
    1. background-color: rgb(0, 0, 255);
    To copy to clipboard, switch view to plain text mode 

    is there a simple way to get background-color in QColor format? I don't want to convert the string to color.

    best wishes.

    Micky Jhon

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get background color of QPushbutton

    You could try through the button's palette. see QWidget:alette() and QPalette

    Cheers,
    _

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

    MickyJhon (7th February 2014)

  4. #3
    Join Date
    Jan 2014
    Posts
    16
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get background color of QPushbutton

    hi ,anda_skoa :

    i use code :

    Qt Code:
    1. curveColor=ui->btn_curveColor->palette().button().color();
    2. //curveColor=ui->btn_curveColor->palette().color(QPalette::Button); // have the same effect as the above.
    3. int r=curveColor.red();
    4. int g=curveColor.green();
    5. int b=curveColor.blue();
    6. int a=curveColor.alpha();
    7. cout<<"r="<<r<<"\tg="<<g<<"\tb="<<b<<"\ta="<<a<<endl;
    To copy to clipboard, switch view to plain text mode 

    however, i only get the output result :

    Qt Code:
    1. r=236 g=233 b=216 a=255
    To copy to clipboard, switch view to plain text mode 

    that's the push-button's default background color, not the result
    Qt Code:
    1. r=0 g=0 b=255 a=255
    To copy to clipboard, switch view to plain text mode 
    as i except. how can i do?

    best wishes.

    Micky Jhon
    Quote Originally Posted by anda_skoa View Post
    You could try through the button's palette. see QWidget:alette() and QPalette

    Cheers,
    _

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get background color of QPushbutton

    Have you tried the Background or Window roles?

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    MickyJhon (7th February 2014)

  7. #5
    Join Date
    Jan 2014
    Posts
    16
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get background color of QPushbutton

    hello, anda_skoa:

    According to your opinion ,i use Background or Window roles. however, i don't get the result as i except.

    my code is :

    Qt Code:
    1. QPalette::ColorRole role=ui->btn_curveColor->backgroundRole();
    2. curveColor=ui->btn_curveColor->palette().color(role);
    3. int r=curveColor.red();
    4. int g=curveColor.green();
    5. int b=curveColor.blue();
    6. int a=curveColor.alpha();
    7.  
    8. QString role1=ui->btn_curveColor->windowRole();
    9.  
    10. cout<<"Use backgroundRole : r="<<r<<"\tg="<<g<<"\tb="<<b<<"\ta="<<a<<endl;
    11. cout<<"Use windowRole : "<<role1.toAscii().data()<<endl;
    To copy to clipboard, switch view to plain text mode 

    I get :
    Qt Code:
    1. Use backgroundRole : r=236 g=233 b=216 a=255
    2. Use windowRole :
    To copy to clipboard, switch view to plain text mode 

    in debug mode. i check the value of role is Button(1), and the role1 is a empty string.

    help me please.

    Micky Jhon




    Quote Originally Posted by anda_skoa View Post
    Have you tried the Background or Window roles?

    Cheers,
    _

  8. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get background color of QPushbutton

    Seems the style sheet does not change the palette but somehow overrides it.

    Too bad, seems you have to parse the stylesheet string yourself.

    Cheers,
    _

  9. #7
    Join Date
    Jan 2014
    Posts
    16
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get background color of QPushbutton

    hi, anda_skoa.thank you all the same. i have to set the flat and AutoFillBackground attributes to true .so i get the result as i except.

    code is :
    Qt Code:
    1. ui->setupUi(this);
    2. ui->btn_curveColor->setFlat(true);
    3. ui->btn_curveColor->setAutoFillBackground(true);
    4. curveColor=ui->btn_curveColor->palette().color(QPalette::Button);
    5.  
    6. int r=curveColor.red();
    7. int g=curveColor.green();
    8. int b=curveColor.blue();
    9. int a=curveColor.alpha();
    10.  
    11. cout<<"r="<<r<<"\tg="<<g<<"\tb="<<b<<"\ta="<<a<<endl;
    To copy to clipboard, switch view to plain text mode 

    best wishes.

    Micky Jhon



    Quote Originally Posted by anda_skoa View Post
    Seems the style sheet does not change the palette but somehow overrides it.

    Too bad, seems you have to parse the stylesheet string yourself.

    Cheers,
    _

Similar Threads

  1. Change background color of QPushButton
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2013, 11:23
  2. QPushbutton, background-color but flat ?
    By gab74 in forum Qt Programming
    Replies: 4
    Last Post: 15th December 2011, 11:15
  3. Background Color of QToolButton or QPushButton
    By metdos in forum Qt Programming
    Replies: 5
    Last Post: 17th June 2010, 12:02
  4. QPushButton color and background
    By satanowicz in forum Newbie
    Replies: 4
    Last Post: 25th May 2010, 21:56
  5. QPushbutton background color
    By omega36 in forum Qt Programming
    Replies: 27
    Last Post: 31st October 2008, 13:47

Tags for this Thread

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.