QLabel Background Color



  • 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



  • Use the pallette, or a style sheet.



  • 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);
    @



  • 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 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.



  • 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



  • Hi, you can easily set the appearance with styleSheet



  • As Clochydd wrote it is easy just like that:
    @title->setStyleSheet("background-color: rgb(85, 170, 255);")@



  • or - if you prefer colour names:

    @

    title->setStyleSheet("background-color: red;")
    title->setStyleSheet("background-color: cornsilk;")

    @



  • That is a bit better, thank you.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.