Qt的QSS官方文档中关于属性选择器有如下的描述
按照官方文档中的描述匹配一个属性完全没问题,但如果需要同时匹配多个属性该如何处理呢?官方文档没有给出具体的方法,经过测试,按照QPushButton[property1="value1"][property2="value2"]的方式可以同时匹配多个属性,下面举个栗子:
mybutton.h
#ifndef MYBUTTON_H
#define MYBUTTON_H
#include <QPushButton>
class MyButton : public QPushButton
{
Q_OBJECT
Q_PROPERTY(QString type MEMBER m_type WRITE setType)
Q_PROPERTY(int number MEMBER m_number WRITE setNumber)
public:
explicit MyButton(QWidget *parent = 0);
void setType(const QString &type);
void setNumber(int number);
private:
QString m_type;
int m_number;
};
#endif // MYBUTTON_H
mybutton.cpp
#include "mybutton.h"
MyButton::MyButton(QWidget *parent)
: QPushButton(parent)
, m_number(1)
{
}
void MyButton::setType(const QString &type)
{
this->m_type=type;
}
void MyButton::setNumber(int number)
{
this->m_number=number;
}
mainwidget.h
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
namespace Ui {
class MainWidget;
}
class MainWidget : public QWidget
{
Q_OBJECT
public:
explicit MainWidget(QWidget *parent = 0);
~MainWidget();
private slots:
void on_buttonSetup_clicked();
private:
Ui::MainWidget *ui;
};
#endif // MAINWIDGET_H
mainwidget.cpp
#include "mainwidget.h"
#include "ui_mainwidget.h"
MainWidget::MainWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWidget)
{
ui->setupUi(this);
ui->button_AA_11->setType("AA");
ui->button_AA_11->setNumber(11);
ui->button_AA_22->setType("AA");
ui->button_AA_22->setNumber(22);
ui->button_BB_11->setType("BB");
ui->button_BB_11->setNumber(11);
}
MainWidget::~MainWidget()
{
delete ui;
}
void MainWidget::on_buttonSetup_clicked()
{
this->setStyleSheet(ui->textStyleSheet->toPlainText());
}
运行效果(点击Setup按钮后的效果):
(------------完----------)