Results 1 to 4 of 4

Thread: QLabel background color

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QLabel background color

    Hi,

    I have a widget whose background color is set to white using

    setBackgroundRole(QPalette::Light);

    This widget has a few QLabels on it. These label should change its text color and the background to some other color when mouse over and should have white background and darkGray text when mouse is not over it. For this I have written the following code.

    Qt Code:
    1. void ViewLabel::enterEvent(QEvent *e)
    2. {
    3. QLabel::enterEvent(e);
    4. QPalette pal;
    5. //setBackgroundRole(QPalette::Dark);
    6. pal.setColor(QPalette::WindowText,QColor(Qt::white));
    7. pal.setColor(QPalette::Base,QColor(Qt::darkGray));
    8. pal.setColor(QPalette::Window,QColor(Qt::darkGray));
    9. setPalette(pal);
    10. }
    11.  
    12. void ViewLabel::leaveEvent(QEvent *e)
    13. {
    14. QLabel::leaveEvent(e);
    15. QPalette pal;
    16. //setBackgroundRole(QPalette::Light);
    17. pal.setColor(QPalette::WindowText,QColor(Qt::darkGray));
    18. pal.setColor(QPalette::Base,QColor(Qt::white));
    19. pal.setColor(QPalette::Window,QColor(Qt::white));
    20. setPalette(pal);
    21. }
    To copy to clipboard, switch view to plain text mode 

    With the above code when mouse goes over the label the background changes to white but the text color is not changing to darkGray. Same thing happens when I use setBackgroundRole.

    Can someone please tell me why the text color is not changing ?

    Thanks a lot.

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QLabel background color

    IMHO it becouse that you set it to white at this line
    Qt Code:
    1. pal.setColor(QPalette::WindowText,QColor(Qt::white));
    To copy to clipboard, switch view to plain text mode 
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel background color

    yes the text color is changing to white but the background color is not changing to darkGray.

    Why is that so?

    Thanks a lot.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel background color

    Try adding "setAutoFillBackground( true )".

  5. The following 2 users say thank you to jacek for this useful post:

    Lemming (29th May 2006), munna (1st May 2006)

Similar Threads

  1. Change QPushButton Background Color, Qt4.3
    By Rayven in forum Qt Programming
    Replies: 5
    Last Post: 4th July 2009, 07:14
  2. button background color when it is clicked
    By navi1084 in forum Qt Programming
    Replies: 4
    Last Post: 9th December 2008, 15:02
  3. how to change QTextEdit background color ?
    By mismael85 in forum Qt Programming
    Replies: 9
    Last Post: 26th June 2008, 22:05
  4. Menu bar background color focus
    By user_mail07 in forum Qt Tools
    Replies: 0
    Last Post: 18th May 2007, 01:25
  5. 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.