Results 1 to 20 of 20

Thread: Dynamically changing QLabel background colour

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.

  2. #2
    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

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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 

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


  5. #4
    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?

  6. #5
    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?

  7. #6
    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

  8. #7
    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.

  9. #8
    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

  10. #9
    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.

  11. #10
    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

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