Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLabel Background Color
Forum Updated to NodeBB v4.3 + New Features

QLabel Background Color

Scheduled Pinned Locked Moved General and Desktop
10 Posts 6 Posters 47.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PowersOf12
    wrote on 7 Mar 2011, 14:00 last edited by
    #1

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on 7 Mar 2011, 14:05 last edited by
      #2

      Use the pallette, or a style sheet.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on 7 Mar 2011, 14:10 last edited by
        #3

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

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • P Offline
          P Offline
          PowersOf12
          wrote on 7 Mar 2011, 14:22 last edited by
          #4

          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.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on 7 Mar 2011, 14:27 last edited by
            #5

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

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BasicPoke
              wrote on 29 Apr 2014, 20:25 last edited by
              #6

              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

              1 Reply Last reply
              0
              • C Offline
                C Offline
                clochydd
                wrote on 29 Apr 2014, 21:01 last edited by
                #7

                Hi, you can easily set the appearance with styleSheet

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  qxoz
                  wrote on 30 Apr 2014, 06:23 last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    clochydd
                    wrote on 30 Apr 2014, 08:05 last edited by
                    #9

                    or - if you prefer colour names:

                    @

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

                    @

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      BasicPoke
                      wrote on 30 Apr 2014, 13:29 last edited by
                      #10

                      That is a bit better, thank you.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved