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 How to change background color and have a border without stylesheet?
Forum Updated to NodeBB v4.3 + New Features

QLabel How to change background color and have a border without stylesheet?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 2.6k Views 2 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.
  • I Offline
    I Offline
    Infestor
    wrote on 11 Apr 2021, 14:38 last edited by
    #1

    Hi,

    So i want to have a QLabel with a certain background color aswell as a black border. Using stylsheets this is done in one line, however i was wondering if it is possible without it and how this would be done.

                QLabel* l = new QLabel();
                      
                QPalette palette;
                palette.setColor(QPalette::Window, Qt::red);
               
                l->setPalette(palette);
                l->setAutoFillBackground(true);
                l->setFixedSize(200, 100);
                l->setText("text");
    

    This is how one can change the background color. How would i now add a black border?

    A 1 Reply Last reply 11 Apr 2021, 16:24
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 11 Apr 2021, 15:40 last edited by
      #2

      Hi
      Well you could put it inside a QFrame and have it that way
      or simply make a QLabel subclass that does as you want.

      #include <QLabel>
      
      class BorderLabel : public QLabel
      {
          Q_OBJECT
      
      public:
          explicit BorderLabel(QWidget *parent = Q_NULLPTR) : QLabel(parent)
          {
          }
      protected:
          virtual void paintEvent(QPaintEvent *event) override
          {
              QPainter p(this);
              p.setPen(QPen(Qt::black, 2));
              p.setBrush(Qt::red);
              p.drawRect(0, 0, width(), height());
      
              QLabel::paintEvent(event); // draw normally
      
          }
      };
      
      
      

      alt text

      1 Reply Last reply
      3
      • I Infestor
        11 Apr 2021, 14:38

        Hi,

        So i want to have a QLabel with a certain background color aswell as a black border. Using stylsheets this is done in one line, however i was wondering if it is possible without it and how this would be done.

                    QLabel* l = new QLabel();
                          
                    QPalette palette;
                    palette.setColor(QPalette::Window, Qt::red);
                   
                    l->setPalette(palette);
                    l->setAutoFillBackground(true);
                    l->setFixedSize(200, 100);
                    l->setText("text");
        

        This is how one can change the background color. How would i now add a black border?

        A Offline
        A Offline
        Alvein
        wrote on 11 Apr 2021, 16:24 last edited by
        #3

        @Infestor Do this below your code

        QFrame *f=qobject_cast<QFrame *>(l);
        f->setFrameShape(QFrame::Shape::Box);
        f->setLineWidth(1);
        
        1 Reply Last reply
        2
        • I Offline
          I Offline
          Infestor
          wrote on 11 Apr 2021, 17:03 last edited by
          #4

          Thank you this worked.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SimonSchroeder
            wrote on 12 Apr 2021, 08:38 last edited by
            #5

            I would really suggest to go with a stylesheet. I don't see a reason why not (never had any issues that it would be too slow or anything).

            Changing the palette might work for some widget types on some platforms. If you use the native style on Windows or Mac OS you will eventually run into problems that you cannot change any part of the palette to influence some of the colors. You could try to use proxy styles to change colors of certain widgets. But, this is very messy compared to just plain stylesheets. Qt used to have setBackgroundColor() at one point, but it slowly faded out. So, as long as you don't have any specific good reason to not use stylesheets, go with stylesheets as they will make your life a lot easier. (I personally wish as well that we did not have to use stylesheets. After fighting it for quite a while I finally gave in because there is no better way in Qt.)

            1 Reply Last reply
            0

            2/5

            11 Apr 2021, 15:40

            • Login

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