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
    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,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: 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,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: 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,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: 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,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: 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?

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.