Solved Set background color of QLabel using Qt Style Sheet and QColorDialog
-
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
-
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);
-
@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. -
@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?
- I just don't know how to pass that rgba variable to that function.
-
QString str = " background-color : " ; QColor col (255,125,255);//takes rgb values str += col.name(); ui->label->setStyleSheet(str);