一般的QML部件都有与之对应的cpp类。
QML部件的属性以及信号都可以从Qt assistant中查询,但是助手中的文档对于属性以及信号的解释并不完整。我们可以通过查看源码的方式来查看cpp类中的属性和信号。
通过在qml中输出部件的id的方式来获取对应cpp文件的名字。
Repeater{
id:repeater
// model: ["m3/M", "60", "65", "70", "75",
// "80", "85", "90","95","100","105",
// "110","115","120","125","130","135"]
model:1
Rectangle{
id:shellRect
width:root.cellWidth
height: root.cellHeight
border.width: 1
TextInput {
id: cell
anchors.fill: parent
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
text: modelData
Component.onCompleted: {
console.log("Repeater:" + repeater)
console.log("Rectangle:" + shellRect)
console.log("TextInput:" + cell)
}
}
}
}
输出结果为:
qml: Repeater:QQuickRepeater(0x3e22b280)
qml: Rectangle:QQuickRectangle(0x3e22b7c0)
qml: TextInput:QQuickTextInput(0x3e22b880)
然后从Qt源码中查看对应类的实现。
我们到一下路径查找到对应文件。(这里可以使用everything软件来查找对应文件。)
D:\Qt\Qt5.12.0\5.12.0\mingw73_64\include\QtQuick\5.12.0\QtQuick\private\qquickrepeater_p.h
class Q_AUTOTEST_EXPORT QQuickRepeater : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(QVariant model READ model WRITE setModel NOTIFY modelChanged)
Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
Q_PROPERTY(int count READ count NOTIFY countChanged)
Q_CLASSINFO("DefaultProperty", "delegate")
public:
QQuickRepeater(QQuickItem *parent=nullptr);
...
};
在这里可以看到 QQuickRepeater 有三个属性,以及其对应的信号.