1.认识default属性
QML的default属性是我们一直在用,但一开始却没注意的机制(我理解为语法糖)。先看文档说明:
An object definition can have a single default property. A default property is the property to which a value is assigned if an object is declared within another object's definition without declaring it as a value for a particular property.
(谷歌翻译)对象定义可以具有单个默认属性。默认属性是如果在另一个对象的定义中声明一个对象但未将其声明为特定属性的值,则为其分配值的属性。
The default property specifies the property to assign to whenever an explicit property is not specified. It is purely a syntactic simplification, the behavior is identical to specifying the property by name, but it can add a more natural feel in many situations. The default property must be either an object or list property.
(谷歌翻译)默认属性指定了在未指定显式属性时要分配给的属性。纯粹是语法上的简化,其行为与通过名称指定属性相同,但是在许多情况下可以增加自然感。默认属性必须是对象或列表属性。
常见的Item有data默认属性,Gradient有stops默认属性。如果没有这个语法的话,你可能得这样写:
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle{
anchors.fill: parent
gradient: Gradient{
stops: [ //默认属性
GradientStop { position: 0.0; color: "orange" },
GradientStop { position: 0.33; color: "yellow" },
GradientStop { position: 1.0; color: "green" }
]
}
data: [ //默认属性
Rectangle{ width: 20; height: 20; color: "black" }
]
}
}
而通过默认属性机制,才支持我们常用的写法:
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle{
anchors.fill: parent
gradient: Gradient{
GradientStop { position: 0.0; color: "orange" }
GradientStop { position: 0.33; color: "yellow" }
GradientStop { position: 1.0; color: "green" }
}
Rectangle{ width: 20; height: 20; color: "black" }
}
}
要注意的是,自定义类型中定义了default属性的话,会把父类型的default标记覆盖掉,需要写上属性名才能使用。
2.在C++中实现
通过宏 Q_CLASSINFO("DefaultProperty",[属性名]) ,可以把一个属性声明为默认属性,这里借用QML渐变色的定义(就他最短小)。
class ColorGradientStop : public QObject
{
//定义略
};
class ColorGradient : public QObject
{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<ColorGradientStop> stops READ stops)
Q_CLASSINFO("DefaultProperty", "stops") //声明为默认属性
public:
ColorGradient(QObject *parent = 0);
~ColorGradient();
QQmlListProperty<ColorGradientStop> stops();
void doUpdate();
QList<ColorGradientStop *> m_stops;
Q_SIGNALS:
void updated();
};
通过前面的文档描述我们知道,可以把一个对象或列表属性声明为默认属性,上面这个渐变色将 ColorGradientStop 列表声明为了默认属性,这样在QML代码中就不用拿中括号 [ ] 把包含的ColorGradientStop括起来,语法上的进行了简写。
Gradient{
GradientStop { position: 0.0; color: "orange" }
GradientStop { position: 0.33; color: "yellow" }
GradientStop { position: 1.0; color: "green" }
}
3.在QML中实现
QML中声明一个属性比较简单,如果要标记为默认属性,在前面加一个default关键字就行了。这里盗用Qt君的例子。
定义一个Group.qml组件:
import QtQuick 2.12
FocusScope {
property alias title: title.text
default property alias items: colume.children
Text { id: title }
Column {
id: colume
spacing: 10
anchors.top: title.bottom
}
}
使用Group:
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Group {
title: "title"
Rectangle {
width: 100; height: 50;
color: "red"
}
Rectangle {
width: 100; height: 50;
color: "lightblue"
}
}
}
最终效果:
使用default让代码风格更优雅了。
4.参考
示例:https://doc.qt.io/qt-5/qtqml-referenceexamples-default-example.html
博客:https://blog.csdn.net/nicai_xiaoqinxi/article/details/93408611