QLabel Background Color
-
wrote on 7 Mar 2011, 14:00 last edited by
I am running Qt 4.7 on a 4.3 WVGA LCD (480x272) so I do not want the window moving around (setWindowFlags(Qt::FramelessWindowHint) nor do I want the standard title bar. So, I have been experimenting with making a nice titlebar for all my menus (with small icon, title, status, etc.)
So, I added a QLabel and wanted to set the text color and background color to look like a title bar would (say white over black (or blue)). However, the following doesn't appear to work for the background although the documentation states that bgcolor is a valid body property to set. The text color works fine. Any ideas?
title->setTextFormat(Qt::RichText);
title->setText("<body bgcolor=#000000><font color=#FFFFFF>Spectrum Analyzer</font></body>");Thanks, Gary
-
wrote on 7 Mar 2011, 14:05 last edited by
Use the pallette, or a style sheet.
-
wrote on 7 Mar 2011, 14:10 last edited by
The background is set via the window of the palette of the label and don't forget to set the autoFillBackground property of the label.
@
title->setAutoFillBackground(true); // IMPORTANT!
QPalette pal = title->palette();
pal.setColor(QPalette::Window, QColor(Qt::black));
title->setPalette(pal);
@ -
wrote on 7 Mar 2011, 14:22 last edited by
Thanks, I will try that. I just assumed the Palette was a set of allowable colors for the window. like a Palette for a BMP file or LCD peripheral, etc. My intuition with Qt is still evolving.
-
wrote on 7 Mar 2011, 14:27 last edited by
[quote author="PowersOf12" date="1299507740"]Thanks, I will try that. I just assumed the Palette was a set of allowable colors for the window. like a Palette for a BMP file or LCD peripheral, etc. My intuition with Qt is still evolving. [/quote]
That would be a palette like in images with color tables (say GIF).
In Qt world a palette describes the color values for different roles (background, text color, button backgrounds and text and the like) for the disabled, inactive and active state of an element. See the "QPalette docs":http://doc.qt.nokia.com/4.7/qpalette.html for some further explanations.
-
wrote on 29 Apr 2014, 20:25 last edited by
So if I use Volker's method, it requires 3 relatively-long lines to set the background color of a label? Is that the most concise way to do it? Sure would be nice if there was a title->setBackgroundColor(Qt::red); QLabel has 303 functions but nothing like this one. Sorry, I'm getting frustrated.
Ron -
wrote on 29 Apr 2014, 21:01 last edited by
Hi, you can easily set the appearance with styleSheet
-
wrote on 30 Apr 2014, 06:23 last edited by
As Clochydd wrote it is easy just like that:
@title->setStyleSheet("background-color: rgb(85, 170, 255);")@ -
wrote on 30 Apr 2014, 08:05 last edited by
or - if you prefer colour names:
@
title->setStyleSheet("background-color: red;")
title->setStyleSheet("background-color: cornsilk;")@
-
wrote on 30 Apr 2014, 13:29 last edited by
That is a bit better, thank you.