将JavaScript QML项解析为C++;QT
我在QML中有一个JavaScript函数,它创建并返回一个组件(将JavaScript QML项解析为C++;QT,javascript,c++,qt,Javascript,C++,Qt,我在QML中有一个JavaScript函数,它创建并返回一个组件(Item): 我也有一些QML在Mal.QML 中,使用了我制作的自定义C++类: // ... import com.acidic.customclass 1.0 import "AddMyComponent.js" as AddMyComponent ApplicationWindow { visible: true width: 1280 height: 800 CustomClass {
Item
):
<>我也有一些QML在// ...
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++类头:< /P>
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> CytCyC形形色元>代码组成的组件到我的自定义C++类的函数参数?
< p>中,我们得到了从// QObject pointer should work with QML objects
Q_INVOKABLE void receiveComponent(QObject* pObj);
请注意,还有
QString
,QVariant
,QVariantList
,QVariantMap
和其他Qt原语。对于。您应该使用Q_可调用的void-receiveComponent(QObject*pObj)代码>@AlexanderVX成功了!谢谢你,如果你发布它,我愿意接受它作为一个答案。我只是不明白为什么这是被否决的@MohammadKanan Idk也是!!:(@acid只是记不起文档的正确链接,但很少有。我发布了“QML和C++之间的数据类型转换”链接和答案。至少这是一些澄清。此外,我建议@acid trysQ_可调用的void receiveComponent(QQuickItem*pQuickItem)
正如初始值设定项varlist告诉我的,它可能是一个QQuickItem
。一般来说,键入越具体,运行时性能/可读性越好。
// QObject pointer should work with QML objects
Q_INVOKABLE void receiveComponent(QObject* pObj);