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. Set background color of QLabel using Qt Style Sheet and QColorDialog
Qt 6.11 is out! See what's new in the release blog

Set background color of QLabel using Qt Style Sheet and QColorDialog

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 6.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.
  • JimmyMarsJ Offline
    JimmyMarsJ Offline
    JimmyMars
    wrote on last edited by
    #1

    I want to set the background of a label using Qt Style Sheet.
    I don't want to use QPalette.
    The user needs to select the color from the QColorDialog.
    I have set the function setColor() as a slot inside Class SelectColor.
    The variable rgba is declared as type QRgb inside the header file under private access.

    How can I pass in the variable rgba as argument for setStyleSheet() ?

    This is not working, it just set the background color to black:
    selectcolor.cpp:

    ...
    void SelectColor::setColor() {
        QColor color = QColorDialog::getColor(QColor(m_label->text()), this);
        color.fromRgba(rgba);
        m_label->setText(color.name());
        m_label->setStyleSheet("background-color : rgba");
    }
    

    selectcolor.h:

    #ifndef SELECTCOLOR_H
    #define SELECTCOLOR_H
    
    #include <QLabel>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QHBoxLayout>
    #include <QPalette>
    #include <QColor>
    #include <QColorDialog>
    
    class QLabel;
    
    class SelectColor : public QWidget {
        Q_OBJECT
    public:
        SelectColor(QWidget *parent = 0);
    public slots:
        void setColor();
    signals:
        // No signals
    private:
        QLabel *m_label;
        QRgb rgba;
    };
    
    #endif // SELECTCOLOR_H
    
    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by kshegunov
      #2

      Hi JimmyMars ,

      Why not using a QString that combines the background part and the color . name () part

      something like this:

      QString str = " background-color : " ;
      str += color. name();
      ui->label->setStyleSheet(str);
      

      Qt Certified Specialist
      www.edalsolutions.be

      JimmyMarsJ 1 Reply Last reply
      1
      • EddyE Eddy

        Hi JimmyMars ,

        Why not using a QString that combines the background part and the color . name () part

        something like this:

        QString str = " background-color : " ;
        str += color. name();
        ui->label->setStyleSheet(str);
        
        JimmyMarsJ Offline
        JimmyMarsJ Offline
        JimmyMars
        wrote on last edited by
        #3

        @Eddy
        But how do I change the background color using Qt Style sheet?
        I just don't know how to pass that rgba variable to that function.

        mrjjM EddyE 2 Replies Last reply
        0
        • JimmyMarsJ JimmyMars

          @Eddy
          But how do I change the background color using Qt Style sheet?
          I just don't know how to pass that rgba variable to that function.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JimmyMars
          Hi its a little unclear what you ask about ?
          You can construct the stylesheet string as @Eddy shows.
          That will contain the color selected.

          • I just don't know how to pass that rgba variable to that function.
            What function are we talking about?
          1 Reply Last reply
          3
          • JimmyMarsJ JimmyMars

            @Eddy
            But how do I change the background color using Qt Style sheet?
            I just don't know how to pass that rgba variable to that function.

            EddyE Offline
            EddyE Offline
            Eddy
            wrote on last edited by kshegunov
            #5

            @JimmyMars

            QString str = " background-color : " ;
            QColor col (255,125,255);//takes rgb values
            str += col.name();
            ui->label->setStyleSheet(str);
            

            Qt Certified Specialist
            www.edalsolutions.be

            1 Reply Last reply
            3

            • Login

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