Results 1 to 20 of 20

Thread: Dynamically changing QLabel background colour

  1. #1
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Dynamically changing QLabel background colour

    In GroupBox I've placed few QLabels. Now - depending on which of this labels I click - I want to change background colour of one of those QLabel.
    I've tried
    Qt Code:
    1. QPalette palette = label->palette();
    2. if (somethink)
    3. palette.setColor(label->backgroundRole(),QColor(Qt::blue));
    4. else
    5. palette.setColor(label->backgroundRole(),QPalette::Window);
    6. label->setText(someText);
    7. label->setPalette(palette);
    8. label->setAutoFillBackground(true);
    9. label->update();
    To copy to clipboard, switch view to plain text mode 

    In the result - if i click on QLabel which should change background of others - no effect. When label->setAutoFillBackground(true); is set - widget turns him self into black block. When i click on widgets which should change background - then they are changing it to a proper one.

    Then I've tried Style sheets. In places where I was changing palette - now I've placed
    Qt Code:
    1. label->setStyleSheet("QLabel { background-color: blue }");
    To copy to clipboard, switch view to plain text mode 
    and where I reset
    Qt Code:
    1. label->setStyleSheet("");
    To copy to clipboard, switch view to plain text mode 

    No difference.
    PS. I'm coding under Windows XP. My application is using different styles like QPlastique, QWindows, QMotif - under each of them I see no effect from above code.

  2. #2
    Join Date
    Oct 2006
    Location
    Austin, Texas, USA
    Posts
    18
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Dynamically changing QLabel background colour

    Qt Code:
    1. label->setStyleSheet("QLabel { background-color: blue }");
    To copy to clipboard, switch view to plain text mode 
    Have you tried this?

    Qt Code:
    1. label->setStyleSheet("background-color: blue");
    To copy to clipboard, switch view to plain text mode 

    Also, you could create a QSS file where you can specify how the button behaves when you hover and click on it.

  3. #3
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically changing QLabel background colour

    Quote Originally Posted by T4ng10r View Post
    Qt Code:
    1. label->setStyleSheet("QLabel { background-color: blue }");
    To copy to clipboard, switch view to plain text mode 
    and where I reset
    Qt Code:
    1. label->setStyleSheet("");
    To copy to clipboard, switch view to plain text mode 
    Try:

    Qt Code:
    1. "QLabel { background: blue }"
    To copy to clipboard, switch view to plain text mode 

    -Jens

  4. #4
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically changing QLabel background colour

    You can use style sheets as the other folks suggest. But if you want to use palettes, I think what is wrong is where you have:

    Qt Code:
    1. if (somethink)
    2. palette.setColor(label->backgroundRole(),QColor(Qt::blue));
    3. else
    4. palette.setColor(label->backgroundRole(),QPalette::Window);
    To copy to clipboard, switch view to plain text mode 
    Notice in the else branch, you have QPalette::Window which is a role, not a color. So in that line you have 2 roles. Frankly, I'm surprised it even compiles. You need to specify a color, i.e.,
    Qt Code:
    1. palette.setColor(label->backgroundRole(), QColor(Qt::<somecolor>));
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. palette.setColor(
    2. Qt::Active,
    3. label->backgroundRole(),
    4. QColor(Qt::<somecolor>));
    To copy to clipboard, switch view to plain text mode 
    where Qt::Active is a color group.

    Carefully review http://doc.trolltech.com/4.2/qpalette.html

    It should contain all the info on how to use palettes.

    HTH,
    Susan

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

    Default Re: Dynamically changing QLabel background colour

    Quote Originally Posted by smacchia View Post
    Notice in the else branch, you have QPalette::Window which is a role, not a color. So in that line you have 2 roles. Frankly, I'm surprised it even compiles.
    It compiles because QColor is castable from int (and QPalette::Window is an int) - notice that Qt::red is an int as well and you could write:
    Qt Code:
    1. palette.setColor(label->backgroundRole(), Qt::red);
    To copy to clipboard, switch view to plain text mode 

    You don't need to go through the QColor constructor.

  6. #6
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically changing QLabel background colour

    Ah you're correct, forgot about that. But that could still be why there is no color change. And while it the code compiles, is logically incorrect.

    I've been using palettes for this very thing successfully (but I must admit, I've been using it for everything *but* labels). The app I am working on has MANY custom colors for every concievable role/state, so I've had to use the palette extensively.

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

    Default Re: Dynamically changing QLabel background colour

    Quote Originally Posted by smacchia View Post
    And while it the code compiles, is logically incorrect.
    Yes.

    I've been using palettes for this very thing successfully (but I must admit, I've been using it for everything *but* labels). The app I am working on has MANY custom colors for every concievable role/state, so I've had to use the palette extensively.
    Labels are tricky. This works for all styles I could test (excluding aqua and windowsxp):
    Qt Code:
    1. #include <QLabel>
    2. #include <QApplication>
    3.  
    4. int main(int argc, char **argv){
    5. QApplication app(argc, argv);
    6. QLabel label;
    7. label.setText("Testing");
    8. label.setStyleSheet("background-color: red;");
    9. label.setAutoFillBackground(true);
    10. label.show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    This works as well:
    Qt Code:
    1. #include <QLabel>
    2. #include <QApplication>
    3.  
    4. int main(int argc, char **argv){
    5. QApplication app(argc, argv);
    6. QLabel label;
    7. label.setText("Testing");
    8. QPalette pal = label.palette();
    9. pal.setColor(label.backgroundRole(), Qt::red);
    10. label.setPalette(pal);
    11. label.setAutoFillBackground(true);
    12. label.show();
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

  8. The following 2 users say thank you to wysota for this useful post:

    Mesozoic (26th February 2014), smacchia (6th March 2007)

  9. #8
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically changing QLabel background colour

    Good stuff - thanks. I'll be storing this in my arsenal

    I would bet it's
    Qt Code:
    1. label.setAutoFillBackground(true);
    To copy to clipboard, switch view to plain text mode 

    that is the key.
    Last edited by smacchia; 6th March 2007 at 15:18.

  10. #9
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamically changing QLabel background colour

    Hmm, now I see my mistakes. And, yes, without label.setAutoFillBackground(true/false); it didn't worked.
    Thank you all for help.

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

    Default Re: Dynamically changing QLabel background colour

    It worked for me without the auto fill, but I believe this may be required for WindowsXP style.

  12. #11
    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: Dynamically changing QLabel background colour

    Quote Originally Posted by wysota View Post
    It worked for me without the auto fill, but I believe this may be required for WindowsXP style.
    The label is a top level widget and therefore not transparent; "Windows are always filled with QPalette::Window, unless the WA_OpaquePaintEvent or WA_NoSystemBackground attributes are set."
    J-P Nurmi

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

    Default Re: Dynamically changing QLabel background colour

    Correct.
    Here is a final working experiment:
    Qt Code:
    1. #include <QLabel>
    2. #include <QApplication>
    3. #include <QVBoxLayout>
    4.  
    5. int main(int argc, char **argv){
    6. QApplication app(argc, argv);
    7. QWidget wgt;
    8. QVBoxLayout *l = new QVBoxLayout(&wgt);
    9. QLabel *label = new QLabel;
    10. l->addWidget(label);
    11. label->setText("Testing");
    12. // this:
    13. // label.setStyleSheet("background-color: red;");
    14. // or this:
    15. QPalette pal = label->palette();
    16. pal.setColor(label->backgroundRole(), Qt::red);
    17. label->setPalette(pal);
    18. label->setAutoFillBackground(true); // rem this to see the difference
    19. wgt.show();
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

  14. The following user says thank you to wysota for this useful post:


  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: Dynamically changing QLabel background colour

    I have a related issue and thought it might fit into this thread. I have a comboBox that's supposed to 'blink' red when an item is added to it.

    Unfortunately, just getting something that works has proven to be a bit of a pain:

    Qt Code:
    1. MyClass::MyClass()
    2. {
    3. comboBox->setAutoFillBackground(true);
    4. }
    5. static bool useRed = true;
    6. void MyClass::addItem(QString itemText)
    7. {
    8. comboBox->insertItem(-1, itemText);
    9. comboBox->setCurrentIndex(0);
    10. QTimer::singleShot(1, this, SLOT(blink()));
    11. }
    12.  
    13. void myClass::blink()
    14. {
    15. if (useRed){
    16. comboBox->setStyleSheet("background-color: red;");
    17. QTimer::singleShot(500, this, SLOT(blink()));
    18. }else
    19. comboBox->setStyleSheet("background-color: yellow;");
    20.  
    21. comboBox->update();
    22. useRed = !useRed;
    23. }
    To copy to clipboard, switch view to plain text mode 

    first time blink is called the background color changes to red, just as expected. The second time around however, the color goes back to the standard grey and subsequent calls have no effect. Am i missing something here?

  16. #14
    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: Dynamically changing QLabel background colour

    Perhaps i should have started a new thread instead of just appending an old one?

  17. #15
    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: Dynamically changing QLabel background colour

    Could you wrap it as a compilable source file so we could test and see ourselves?
    J-P Nurmi

  18. #16
    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: Dynamically changing QLabel background colour

    Quote Originally Posted by jpn View Post
    Could you wrap it as a compilable source file so we could test and see ourselves?
    Sure thing Jpn!

    header file:
    Qt Code:
    1. #include <QMainWindow>
    2. #include <QWidget>
    3. #include <QPushButton>
    4. #include <QComboBox>
    5. #include <QVBoxLayout>
    6.  
    7. class Gui : public QMainWindow
    8. {
    9. Q_OBJECT
    10. public:
    11. Gui( QWidget * parent = 0 );
    12.  
    13. private slots:
    14. void blinkMe();
    15. void blinkStyle();
    16. void blinkPalette();
    17. private:
    18. QWidget *centralwidget;
    19. QPushButton *pushButton;
    20. QComboBox *comboBox;
    21. QVBoxLayout *layout;
    22. };
    To copy to clipboard, switch view to plain text mode 

    source file:
    Qt Code:
    1. #include <iostream>
    2. #include <QApplication>
    3. #include <QString>
    4. #include <QTimer>
    5.  
    6. #include "gui.h"
    7. //
    8. using namespace std;
    9. //
    10. int main(int argc, char ** argv)
    11. {
    12. QApplication app( argc, argv );
    13. Gui gui;
    14. gui.show();
    15. app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
    16. return app.exec();
    17. }
    18.  
    19. Gui::Gui( QWidget * parent ) : QMainWindow(parent)
    20. {
    21. this->setObjectName(QString::fromUtf8("MainWindow"));
    22. this->setWindowTitle("main window test");
    23. centralwidget = new QWidget(this);
    24. centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    25. this->setCentralWidget(centralwidget);
    26.  
    27. pushButton = new QPushButton(centralwidget);
    28. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    29. pushButton->setText("blink?");
    30. pushButton->setAutoFillBackground(true);
    31.  
    32. comboBox = new QComboBox(centralwidget);
    33. comboBox->setObjectName(QString::fromUtf8("comboBox"));
    34. comboBox->setAutoFillBackground(true);
    35.  
    36. layout = new QVBoxLayout(centralwidget);
    37. layout->addWidget(comboBox);
    38. layout->addWidget(pushButton);
    39.  
    40. QSize size(322, 149);
    41. size = size.expandedTo(this->minimumSizeHint());
    42. this->resize(size);
    43.  
    44. connect(pushButton, SIGNAL(clicked()),
    45. this, SLOT(blinkMe()));
    46.  
    47. }
    48.  
    49. static int blinkCount = 0;
    50. void Gui::blinkMe()
    51. {
    52. cout << "trying to blink the comboBox..." << endl;;
    53.  
    54. blinkStyle();
    55. //blinkPalette();
    56. }
    57.  
    58. void Gui::blinkStyle()
    59. {
    60. if (++blinkCount > 6){
    61. blinkCount = 0;
    62. return;
    63. }
    64.  
    65. QWidget* blinkable = comboBox;
    66.  
    67. if (blinkCount % 2){
    68. //qApp->setStyleSheet("QComboBox#comboBox { background-color: yellow }");
    69. blinkable->setStyleSheet("background-color: yellow");
    70. }else{
    71. //qApp->setStyleSheet("QComboBox#comboBox {}");
    72. blinkable->setStyleSheet("");
    73. }
    74. blinkable->update();
    75.  
    76. QTimer::singleShot(200, this, SLOT(blinkStyle()));
    77.  
    78. cout << "blink style! " << blinkable->styleSheet().toStdString() << endl;
    79. }
    80.  
    81. void Gui::blinkPalette()
    82. {
    83. static QColor oldBaseColor;
    84. static QColor oldHighlightColor;
    85.  
    86. if (++blinkCount > 6){
    87. blinkCount = 0;
    88. return;
    89. }
    90.  
    91. QWidget* blinkable = comboBox;
    92. QPalette palette = blinkable->palette();
    93.  
    94. if (blinkCount % 2){
    95. oldBaseColor = blinkable->palette().color(QPalette::Base);
    96. palette.setColor(QPalette::Base, Qt::yellow);
    97.  
    98. oldHighlightColor = blinkable->palette().color(QPalette::Highlight);
    99. palette.setColor(QPalette::Highlight, Qt::yellow);
    100. }else{
    101. palette.setColor(QPalette::Base, oldBaseColor);
    102. palette.setColor(QPalette::Highlight, oldHighlightColor);
    103. }
    104. blinkable->setPalette(palette);
    105. blinkable->update();
    106.  
    107. QTimer::singleShot(200, this, SLOT(blinkPalette()));
    108.  
    109. cout << "blink palette! " << endl;
    110. }
    111. //
    To copy to clipboard, switch view to plain text mode 

    The above example has three test cases.
    1) blinking the comboBox's style sheet. To do so, make sure blinkMe() calls blinkStyle() and that blinkStyle() sets the blinkable-widget's style sheet. This is the default.

    2) blinking the application's style sheet. Call blinkStyle() and make sure the call to qApp is made instead of to the blinkable-widget.

    3) blinking the comboBox's palette. To do so, call blinkPalette() instead of blinkStyle().

    Expected results:
    All three cases should cause the comboBox's background to blink yellow three times before turning white again.

    Actual results:
    Only the last two cases behave as expected. The first case causes the comboBox to blink yellow only once before turning white again, regardless of what the secondary color is set to.

  19. #17
    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: Dynamically changing QLabel background colour

    Hmm, maybe a problem with parsing/applying the style sheet? Passing the same style sheet to the combo box than is passed to the application works also.

    Edit: Oh, and by the way, please attach large files instead of pasting them as code blocks. Copy-pasting from the code block is inconvenient because of all the extra line number and line breaks.
    Last edited by jpn; 19th April 2007 at 11:06.
    J-P Nurmi

  20. #18
    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: Dynamically changing QLabel background colour

    Quote Originally Posted by jpn View Post
    Hmm, maybe a problem with parsing/applying the style sheet? Passing the same style sheet to the combo box than is passed to the application works also.
    But it's strange that it won't work properly for the comboBox since setting the pushButton as the blinkable object gives the expected behavior. Is there any obvious reason why applying the stylesheet directly to the comboBox won't work? Did the example behave as expected for you or did you experience the same behavior? Is this a bug perhaps?

    Quote Originally Posted by jpn View Post
    Edit: Oh, and by the way, please attach large files instead of pasting them as code blocks. Copy-pasting from the code block is inconvenient because of all the extra line number and line breaks.
    Ok, thanks for the heads-up, i'll attach files in the future.

  21. #19
    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: Dynamically changing QLabel background colour

    Quote Originally Posted by TheRonin View Post
    But it's strange that it won't work properly for the comboBox since setting the pushButton as the blinkable object gives the expected behavior. Is there any obvious reason why applying the stylesheet directly to the comboBox won't work? Did the example behave as expected for you or did you experience the same behavior? Is this a bug perhaps?
    Yeah, I experienced the same and I'd call it a bug. Anyway, what I meant with my previous post was that for me it actually works directly for the combobox in case the style sheet is passed in a same way than you pass it to the application eg. including the selector:
    Qt Code:
    1. // blinkable->setStyleSheet("background-color: yellow");
    2. blinkable->setStyleSheet("QComboBox{background-color: yellow}"); // <-- works
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  22. #20
    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: Dynamically changing QLabel background colour

    You're right, that does work. I'll do it that way for now then. Thanks!

Similar Threads

  1. Dynamically changing QFrame color
    By Doug Broadwell in forum Newbie
    Replies: 6
    Last Post: 16th October 2008, 08:22
  2. QLabel background color
    By munna in forum Newbie
    Replies: 3
    Last Post: 1st May 2006, 15:36
  3. background colour
    By kw in forum Qt Programming
    Replies: 6
    Last Post: 11th April 2006, 00:44
  4. Replies: 1
    Last Post: 5th April 2006, 16:44

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.