C++ 我有多个通过StackView推送的qml文件。如何将它们连接到C++;
我的项目包含6个qml文件:main.qml打开一个新的应用程序窗口并声明工具栏。它还使用initalItem homescreen.qml初始化StackView。在主屏幕上,我有不同的按钮,通过stack.push(“URL”)打开不同的qml文件。除了main.qml之外,所有文件都以{}项开头。 我已经能够连接main.qml和home.qml的信号。但我无法访问堆栈中更深的对象。我不知道是否需要更改.cpp代码以访问其他对象,或者是否应该更改StackView的初始化,以便在开始时加载并访问所有文件。 下面是代码,分解到最基本的部分:C++ 我有多个通过StackView推送的qml文件。如何将它们连接到C++;,c++,qt,qml,interaction,stackview,C++,Qt,Qml,Interaction,Stackview,我的项目包含6个qml文件:main.qml打开一个新的应用程序窗口并声明工具栏。它还使用initalItem homescreen.qml初始化StackView。在主屏幕上,我有不同的按钮,通过stack.push(“URL”)打开不同的qml文件。除了main.qml之外,所有文件都以{}项开头。 我已经能够连接main.qml和home.qml的信号。但我无法访问堆栈中更深的对象。我不知道是否需要更改.cpp代码以访问其他对象,或者是否应该更改StackView的初始化,以便在开始时加载
- main.qml
}ApplicationWindow { Rectangle{ id: homeButton objectName: "homeButton" signal qmlSignal(string msg) MouseArea { onClicked: {stack.push({item:"qrc:/home.qml}); homeButton.qmlSignal("Hello")} } } StackView{ initalItem: "qrc:/home.qml" }
- secondframe.qml//主屏幕后面的随机w qml文件
Item { Rectangle{ id: test objectName: "test" signal qmlSignal(string msg) MouseArea { onClicked: {stack.push({item:"qrc:/thirdframe.qml}); test.qmlSignal("Hello")} } } }
- main.cpp
QApplication app (argc, argv); QQmlEngine enigne; QQmlComponent component(&engine, QUrl(QStringLiteral("qrl:/main.qml"))); QObject *object = componet.create(); QQmlComponent newcomponent(&engine, QUrl(QStringLiteral("qrl:/secondframe.qml"))); QObject *newobject = newcomponet.create(); MyClass myClass QObject *home = object->findChild<QObject*>("homeButton"); // I'm able to connect to every Object in the main.qml or home.qml QObject::connect(home,SIGNAL(qmlSignal(Qstring)), &myClass, SLOT(cppSlot(QString))); QObject *test = newobject->findChild<QObject*>("test"); // Can't connect to the Objects in secondframe.qml QObject::connect(test,SIGNAL(qmlSignal(Qstring)), &myClass, SLOT(cppSlot(QString)));
QApplication应用程序(argc、argv); QQmlEngine-enigne; QQmlComponent组件(&引擎,QUrl(QStringLiteral(“qrl:/main.qml”)); QObject*object=componet.create(); QQmlComponent新组件(&引擎,QUrl(QStringLiteral(“qrl:/secondframe.qml”)); QObject*newobject=newcomponet.create(); 我的班级我的班级 QObject*home=object->findChild(“homeButton”);//我能够连接到main.qml或home.qml中的每个对象 QObject::connect(主,信号(qmlSignal(Qstring)),&myClass,插槽(cppSlot(Qstring)); QObject*test=newobject->findChild(“test”);//无法连接到secondframe.qml中的对象 QObject::connect(测试,信号(qmlSignal(Qstring)),&myClass,插槽(cppSlot(Qstring));
一种更好的方法,而不是进入QML树并拔出可能存在或不存在的对象,是将基于C++的API提供给QML。
Q\u INVOKABLE调用的方法
class MyAPI : public QObject
{
Q_OBJECT
public slots:
void cppSlot(const QString &text);
};
MyAPI myApi;
QQmlEngine engine;
engine.rootContext()->setContextProperty("_cppApi", &myApi);
MouseArea {
onClicked: {stack.push({item:"qrc:/thirdframe.qml}); _cppApi.cppSlot("Hello")}
}
在堆栈上推送QML后,对象将可访问。所以,当你们发出一些信号时,当对象在堆栈上时,解决方案可以如下所示:
MyClass myClass
QObject *home = object->findChild<QObject*>("homeButton"); // I'm able to connect to every Object in the main.qml or home.qml
QObject::connect(home,SIGNAL(qmlSignal(Qstring)), &myClass, SLOT(cppSlot(QString)));
connect( myClass, MyClass::emitWhenQMLIsAlreadyOnTheStack, this, [this](){
QObject *test = newobject->findChild<QObject*>("test"); // Now you should be able to connect
QObject::connect(test,SIGNAL(qmlSignal(Qstring)), &myClass, SLOT(cppSlot(QString));
});
MyClass MyClass
QObject*home=object->findChild(“homeButton”);//我能够连接到main.qml或home.qml中的每个对象
QObject::connect(主,信号(qmlSignal(Qstring)),&myClass,插槽(cppSlot(Qstring));
连接(myClass,myClass::emitWhenqmlisalreadyontStack,this,[this](){
QObject*test=newobject->findChild(“test”);//现在您应该可以连接了
QObject::connect(测试,信号(qmlSignal(Qstring)),&myClass,插槽(cppSlot(Qstring));
});
非常感谢!!!你是一位编程重量级冠军,也是一位非常美丽的人。我很高兴这一切都很好。感谢你花时间研究我的问题,向你展示这不是只有重量级编程冠军才知道的秘密。我向你展示了Allwighty Qt文档:有一些工作正在进行中特定文档更容易访问/发现,但我认为听听用户的想法还是不错的;文档如何改进,他们是否能够找到它,等等。建议总是受欢迎的:bugreports.qt。io@Mailerdaimon这实际上是我使用的页面,它对我的特殊问题没有帮助QObject::connect没有使我能够访问启动时未加载的文件。通过Kevins将类传递给QML的解决方案,我能够与我想要的每个对象进行交互。也许我在使用::connect方法时犯了一个错误,但无论如何,Kevin的解决方案没有包含在帮助页面中。OP:可能最好看看。@Mitch文档在每个版本之间不断改进。有时,它缺少的是某种“最佳实践”或“一般建议”。类似于但适用于快速主题的内容(例如,上下文属性与注册类型)对新手非常有用。仅0.02美分。:)secondframe.qml如何访问堆栈对象?我有一个应用程序的Stackview有n个页面。我需要从一页导航到另一页。我该怎么做?