通过查看Qt源码,了解QML部件的属性以信号

一般的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 有三个属性,以及其对应的信号.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值