Qt下自定义QSS属性

SelfProperty.h

#ifndef SELFPROPERTY_H
#define SELFPROPERTY_H
#include <QLabel>
#include <QWidget>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
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 <QStyle>
#include <QVariant>
#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 <QApplication>
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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值