(参见 Qt 文档页章节 Setting QObject Properties:https://doc.qt.io/qt-5/stylesheet-syntax.html)
从 Qt 4.3 及更高版本开始,可以使用 qproperty- 语法设置任何 designable 的 Q_PROPERTY ,QWidget 派生类支持该语法,仅继承 QObject 不会生效。使用该机制可以很方便的扩展我们的程序。
如我们给自己的窗口加上属性:
class MainWindow : public QMainWindow
{
Q_OBJECT
Q_PROPERTY(int id READ getId WRITE setId)
Q_PROPERTY(QString name READ getName WRITE setName)
//... ...
}
就可以在 QSS 样式表中这样赋值:
(QString 类型的貌似不能空格分隔,而且不能和颜色值之类的冲突)
MainWindow{
qproperty-id:10;
qproperty-name:gongjianbo-1992;
}
如果我们在属性的 set 函数中打印的话,是可以看到对应输出的:
一些常用的类型对应的 QSS 写法:
/* QIcon QImage 等 */
MyLabel { qproperty-pixmap: url(pixmap.png); }
/* QColor */
MyGroupBox { qproperty-titleColor: rgb(100, 200, 100); }
MyPushButton {
/* QSize */
qproperty-iconSize: 20px 20px;
/* QRect */
qproperty-myrect:rect(10 20 100 50);
/* QBrush 之渐变色 */
qproperty-mycolor:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));
}
2.注意事项
如果该属性引用用 Q_ENUMS 声明的枚举,则应按名称(而不是其数值)引用其常量。
请谨慎使用 qproperty 语法,因为它会修改正在绘制的小部件。
qproperty 语法仅计算一次,即用样式修饰小部件时。这意味着在伪状态(例如 QPushButton:hover)中使用它们的任何尝试都将无效。但是我们可以给非伪状态重新设置属性值。
如果使用 QString 类型,不要和 QColor 之类的属性值冲突,并且中间不要有分隔。
对于渐变色,可以用 QBrush 类接收。
(2020-6-6补充)正数赋值直接写数字就行,负数测试发现需要加字符串引号。
3.样式表与枚举(2020-6-6补充)对于自定义属性的枚举值,赋值操作使用字符串:
class MyLabel : public QLabel
{
Q_OBJECT
Q_ENUMS(MyColor)
Q_PROPERTY(MyColor color READ getColor WRITE setColor)
public:
enum MyColor{
Green=1,
Yellow=2,
Red=3
};
//... ...
}
/*类选择器*/
.MyLabel{
/*这里有作用域是因为我枚举是声明在MyLabel类里的*/
qproperty-color:"MyLabel::Red";
}
对于属性选择器中的枚举值,赋值使用枚举对应常量的字符串:
.MyLabel[color="1"]{
background:green;
}
.MyLabel[color="2"]{
background:yellow;
}
.MyLabel[color="3"]{
background:red;
}
经测试 Q_ENUMS 和 Q_ENUM 注册的枚举是一样的写法,虽然 QDebug 打印的时候前者是常量后者是枚举字符串。(Q_ENUMS 和 Q_ENUM 可以参见文档,以及相关资料:https://woboq.com/blog/q_enum.html)
4.相关源码片段属性解析相关
//E:\Qt\qt-everywhere-src-5.15.0\qtbase\src\widgets\styles\qstylesheetstyle.cpp
(可以看到一些支持的基本类型)
void QStyleSheetStyle::setProperties(QWidget *w)
{
// The final occurrence of each property is authoritative.
// Set value for each property in the order of property final occurrence
// since properties interact.
const QVector decls = declarations(styleRules(w), QString());
QVector finals; // indices in reverse order of each property's final occurrence
{
// scan decls for final occurrence of each "qproperty"
QSet propertySet;
for (int i = decls.count() - 1; i >= 0; --i) {
const QString property = decls.at(i).d->property;
if (!property.startsWith(QLatin1String("qproperty-"), Qt::CaseInsensitive))
continue;
if (!propertySet.contains(property)) {
propertySet.insert(property);
finals.append(i);
}
}
}
for (int i = finals.count() - 1; i >= 0; --i) {
const Declaration &decl = decls.at(finals[i]);
QString property = decl.d->property;
property.remove(0, 10); // strip "qproperty-"
const QMetaObject *metaObject = w->metaObject();
int index = metaObject->indexOfProperty(property.toLatin1());
if (Q_UNLIKELY(index == -1)) {
qWarning()
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?