【发布时间】:2017-08-25 15:23:39
【问题描述】:
这是我的代码(无法编译,在 main.cpp 中标记连接时说:“在‘,’标记之前预期主表达式”)
main.cpp
QObject *myButton = engine.rootObjects().first() -> findChild<QObject*>("btn");
QObject::connect(myButton, SIGNAL(clicked()), MyClass, SLOT(MyClass()));
main.qml
ApplicationWindow {
id: appWindow
visible: true
width: 850; height: 500
Button
{
objectName: btn
anchors.centerIn: parent
}
}
我希望每次单击按钮时都实例化 cpp 类 MyClass。
我之前做了一件不同的事情,但是它在应用程序运行时实例化 MyClass 的问题,而不是在单击按钮时实例化的问题。此外,我不能调用构造函数,只能调用 Q_INVOKABLE 公共方法。
main.cpp
MyClass myClass;
engine.rootContext() -> setContextProperty("_btn", &myClass);
main.qml
ApplicationWindow {
id: appWindow
visible: true
width: 850; height: 500
Button
{
objectName: btn
anchors.centerIn: parent
onClicked: _btn.myMethod() //where myMethod is a Q_INVOKABLE public method belonging to MyClass.
}
}
【问题讨论】:
标签: c++ qt qml signals-slots