You can retrieve colors in code that were set in the stylesheet, however I don't think you can do it in the constructor as the style sheet may not have been read yet.
I have been capturing them in the showEvent:
Example:
{
Q_OBJECT
public:
protected:
private:
}
{
static bool first = true; // Prevent constant re-querying
if (first)
{
bgColor
= pal.
color(QPalette::Active,
QPalette::Window);
// background-color in stylesheet altBGColor
= pal.
color(QPalette::Active,
QPalette::AlternateBase);
//alternate-background-color in stylesheet // NOTE: Highlighted foreground and background are also available
first = false;
}
// ...
}
class MyWidget: public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget* par);
protected:
void showEvent(QShowEvent* evt);
private:
QColor fgColor;
QColor bgColor;
QColor altBGColor;
}
void MyWidget::showEvent(QShowEvent* evt)
{
static bool first = true; // Prevent constant re-querying
if (first)
{
QPalette pal = palette();
fgColor = pal.color(QPalette::Active, QPalette::Text); // color in stylesheet
bgColor = pal.color(QPalette::Active, QPalette::Window); // background-color in stylesheet
altBGColor = pal.color(QPalette::Active, QPalette::AlternateBase); //alternate-background-color in stylesheet
// NOTE: Highlighted foreground and background are also available
first = false;
}
// ...
}
To copy to clipboard, switch view to plain text mode
That's it... Your style Sheet might look like this for MyWidget:
MyWidget
{
color: #ffffff;
background-color: #000000;
alternate-background-color: #200000;
}
MyWidget
{
color: #ffffff;
background-color: #000000;
alternate-background-color: #200000;
}
To copy to clipboard, switch view to plain text mode
I hope this helps.
Bookmarks