Qt下自定义QSS属性

SelfProperty.h

#ifndef SELFPROPERTY_H
#define SELFPROPERTY_H
#include 
#include 
#include 
#include 
#include 
enum class emType
{
    P_Red=0,
    P_Blue,
    P_Green
};
class SelfProperty : public QWidget
{
    Q_OBJECT
private:
    QLabel* labProperty;
    QPushButton* btnProperty1;
    QPushButton* btnProperty2;
    QPushButton* btnProperty3;
    QHBoxLayout* btnLayout;
    QVBoxLayout* mainLayout;
private:
    void OnSetColor(const emType& eColor);
public:
    SelfProperty(QWidget *parent = nullptr);
    ~SelfProperty();
};
#endif // SELFPROPERTY_H

SelfProperty.cpp

#include 
#include 
#include "SelfProperty.h"
SelfProperty::SelfProperty(QWidget *parent)
    : QWidget(parent)
{
    setFixedSize(400,300);
    labProperty=new QLabel(this);
    btnProperty1=new QPushButton(this);
    btnProperty2=new QPushButton(this);
    btnProperty3=new QPushButton(this);
    btnLayout=new QHBoxLayout;
    mainLayout=new QVBoxLayout();
    btnProperty1->setText("Property1");
    btnProperty2->setText("Property2");
    btnProperty3->setText("Property3");
    labProperty->setObjectName("labProperty");
    btnLayout->setMargin(0);
    btnLayout->setSpacing(10);
    btnLayout->addWidget(btnProperty1);
    btnLayout->addWidget(btnProperty2);
    btnLayout->addWidget(btnProperty3);
    btnLayout->addStretch(1);
    mainLayout->setMargin(10);
    mainLayout->setSpacing(10);
    mainLayout->addLayout(btnLayout);
    mainLayout->addWidget(labProperty);
    setLayout(mainLayout);
    OnSetColor(emType::P_Red);
    connect(btnProperty1,&QPushButton::clicked,this,[=](bool){
        OnSetColor(emType::P_Red);
    });
    connect(btnProperty2,&QPushButton::clicked,this,[=](bool){
        OnSetColor(emType::P_Blue);
    });
    connect(btnProperty3,&QPushButton::clicked,this,[=](bool){
        OnSetColor(emType::P_Green);
    });
}
SelfProperty::~SelfProperty()
{
    if(btnLayout!=Q_NULLPTR){
        delete btnLayout;
        btnLayout=Q_NULLPTR;
    }
    if(mainLayout!=Q_NULLPTR){
        delete mainLayout;
        mainLayout=Q_NULLPTR;
    }
}
void SelfProperty::OnSetColor(const emType& eColor)
{
    switch (eColor)
    {
    case emType::P_Red:
        labProperty->setProperty("color", QVariant("red"));
        style()->polish(labProperty);
        break;
    case emType::P_Blue:
        labProperty->setProperty("color", QVariant("blue"));
        style()->polish(labProperty);
        break;
    case emType::P_Green:
        labProperty->setProperty("color", QVariant("green"));
        style()->polish(labProperty);
        break;
    default:
        labProperty->setProperty("color", QVariant("red"));
        style()->polish(labProperty);
        break;
    }
}

main.cpp

#include "SelfProperty.h"
#include 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //设置样式表
    QString style=R"(QLabel#labProperty[color=red]{
                    background-color: #ff0000;
                  }
                  QLabel#labProperty[color=blue]{
                    background-color: #00ff00;
                  }
                  QLabel#labProperty[color=green]{
                    background-color: #0000ff;
                  })";
    qApp->setStyleSheet(style);
    SelfProperty mainWidget;
    mainWidget.show();
    return a.exec();
}

你可能感兴趣的:(LQ-Other)