解析JavaScript QML项目至C QT

Parse JavaScript QML item to C++ QT

本文关键字:QT 项目 JavaScript QML 解析      更新时间:2023-10-16

我在qml中具有JavaScript函数,该功能创建和返回组件(Item(:

function addMyComponent() {
    var component = Qt.createComponent('MyComponent.qml');
    var obj = component.createObject(container, {'x': 0, 'y': 0});
    return obj; // Not sure weather to return obj or component for my C++ to use
}

我在main.qml中也有一些QML,它使用了我制作的自定义C 类:

// ...
import com.acidic.customclass 1.0
import "AddMyComponent.js" as AddMyComponent
ApplicationWindow {
    visible: true
    width: 1280
    height: 800
    CustomClass {
        id: customClass
    }
    Button {
        onClicked: {
            customClass.receiveComponent(AddMyComponent.addMyComponent)
        }
    }
}

和我的C 类标题:

Q_INVOKABLE void receiveComponent(const QObject& obj /* QObject ref doesn't work */);

和身体:

void CustomClass::receiveComponent(const QObject& obj) {
    qDebug(obj.property("width")); // To see if we have received it correctly
}

如何将用JavaScript创建的组件和Qt.createComponent解析到我的自定义C 类函数参数?

我们具有从 QQuickItem(qt quick(派生的qml ui对象,该对象是 QObject和其他'辅助'对象,也用于从 QObject base得出的QML中使用的QML。

// QObject pointer should work with QML objects
Q_INVOKABLE void receiveComponent(QObject* pObj);

请注意,那里还有QStringQVariantQVariantListQVariantMap和其他QT原语。用于参考。