READ name WRITE ww.laipuhuo.com setName NOTIFY name

70 阅读1分钟
  1. 定义QML类型
    要在QML中使用C++类,首先需要在C++中定义一个元对象系统(MOC)兼容的类。这个类将作为QML类型的基础。例如,
    cpp
    class Person : public QObject {
    Q_OBJECT
    public:
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    __ 构造函数
    Person(QObject *parent = nullptr) : QObject(parent) {}
    signals:
    void nameChanged(const QString &name);
    public:
    www.laipuhuo.com/articlePage… www.laipuhuo.com/articlePage… www.laipuhuo.com/articlePage… www.laipuhuo.com/articlePage… www.laipuhuo.com/articlePage… QString name() const;
    void setName(const QString &name);
    private:
    QString m_name;
    };
  2. 在QML中使用C++类
    一旦在C++中定义了类,就可以在QML中使用它,就像使用QML原生类型一样。这需要通过QML_IMPORT宏来导入C++模块。例如,
    qml
    import QtQuick 2.15
    import QtQuick.Controls 2.15
    QML_IMPORT(MyModule, Person)
    ApplicationWindow {
    visible: true
    width: 400
    height: 300
    Column {