本文介绍了setMainQmlFile`,rootObject和showExpanded不是QQmlApplicationEngine的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我写了这段代码后,一些家伙的教程,但我不能让它运行。错误说:它应该做的是从QML获取一个信号,并打印出一条消息(在控制台中)。基本上我试图集成C ++和QML。 EDIT 我试图用一些似乎合适的其他函数对我来说)。我还试图找到包含什么,以使这些功能将工作,但没有运气。 #include< QGuiApplication> ; #include< QQmlApplicationEngine> #includeqtquickglobal.h #include< QQmlContext> #includemyclass.h #include< QtCore> #include< QtDebug> #include< QQuickWindow> int main(int argc,char * argv []){ // Q_OBJECT; QGuiApplication app(argc,argv); QQmlApplicationEngine查看器; viewer.load(QUrl(QStringLiteral(Qt / Versuch2 / main.qml))); myclass数据; viewer.rootContext() - > setContextProperty(myclassData,& data); viewer.setMainQmlFile(QStringLiteral(qml / Versuch2 / main.qml)); QObject * viewerobject = viewer.rootObject(); QObject :: connect(viewerobject,SIGNAL(qmlSignal(QString)),& data,SLOT(cppSlot(QString))); viewer.showExpanded(); return app.exec(); } void myclass :: cppSlot(QString msg){ qDebug()<< QString消息:%1)。arg(msg); } 感谢。解决方案使用Qt 5.4.0和Qt Creator 3.3.0创建新项目: 点击新建项目 Qb快速应用程序 点击选择... 点击下一步 点击下一步 选择所需的资料袋 点击下一步 点击完成 现在您应该可以看到打开的main.qml文件,包含以下代码: import QtQuick 2.4 import QtQuick.Window 2.2 窗口{ visible:true MainForm { anchors.fill:parent mouseArea.onClicked:{ Qt.quit(); } } } 对文件进行更改,如下所示: import QtQuick 2.4 import QtQuick.Window 2.2 窗口{ visible:true // ###新代码### signal myQmlSignal(string msg) // ################ MainForm { anchors.fill :parent mouseArea.onClicked:{ // ###新代码### //替换Qt.quit(); with console.log(从QML发送myQmlSignal ...); myQmlSignal(Hello from QML) // ################ } } } 鼠标单击项目查看器中的项目名称 单击添加新... 选择C ++类 单击选择... 在类名称输入MyCppClass 将基类设置为QObject 点击下一步 点击完成 打开mycppclass.h文件,应该如下所示: #ifndef MYCPPCLASS_H #define MYCPPCLASS_H #include< QObject> class MyCppClass:public QObject { Q_OBJECT public: explicit MyCppClass(QObject * parent = 0); 〜MyCppClass(); 信号: 公开位置:}; #endif // MYCPPCLASS_H 更改mycppclass.h,如下所示: #ifndef MYCPPCLASS_H #define MYCPPCLASS_H #include< QObject> // ###新代码### #include< QDebug> // ################ class MyCppClass:public QObject { Q_OBJECT public: explicit MyCppClass(QObject * parent = 0); 〜MyCppClass(); 信号: 公共位置: // ###新代码### void myCppSlot(const QString& msg); // ################ }; #endif // MYCPPCLASS_H 打开mycppclass.cpp,应如下所示: #includemycppclass.h MyCppClass :: MyCppClass(QObject * parent):QObject(parent) { } MyCppClass ::〜MyCppClass { } 更改为: #includemycppclass.h MyCppClass :: MyCppClass * parent):QObject(parent) { } MyCppClass ::〜MyCppClass() { } void MyCppClass :: myCppSlot(const QString& msg) { // ###新代码### qDebug )<< C ++接收到信号,其包含以下消息:< msg; // ############### } 打开main.cpp,如下所示: #include< QGuiApplication> #include< QQmlApplicationEngine> int main(int argc,char * argv []) { QGuiApplication app(argc,argv); QQmlApplicationEngine引擎; engine.load(QUrl(QStringLiteral(qrc:/main.qml))); return app.exec(); } #include< QGuiApplication> #include< QQmlApplicationEngine> // ###新代码### #includemycppclass.h // ####### ######### int main(int argc,char * argv []) { QGuiApplication app(argc,argv); QQmlApplicationEngine引擎; engine.load(QUrl(QStringLiteral(qrc:/main.qml))); // ###新代码### MyCppClass myCppClass; QObject :: connect(engine.rootObjects()。takeFirst(),SIGNAL(myQmlSignal(QString)),& myCppClass,SLOT(myCppSlot(QString))); // ################ return app.exec(); } 点击大绿色三角编译和运行应用程序。查看应用程序输出区域,单击Hello World,您应该看到以下消息被打印出来: qml:从QML发送myQmlSignal ... 信号由C ++接收。它包含以下消息:Hello from QML I've written this piece of code following some guys tutorial but I can't get it to run. The error says:What it's supposed to do is get a signal from QML and print out a message (in console). Basically I'm trying to integrate C++ and QML.EDITI've tried to replace some of the functions with some others that seemed appropriate (at least to me). I've also tried to find what to include so that these functions would work but with no luck. #include <QGuiApplication> #include <QQmlApplicationEngine> #include "qtquickglobal.h" #include <QQmlContext> #include "myclass.h" #include <QtCore> #include <QtDebug> #include <QQuickWindow>int main(int argc, char *argv[]){ //Q_OBJECT; QGuiApplication app(argc, argv); QQmlApplicationEngine viewer; viewer.load(QUrl(QStringLiteral("Qt/Versuch2/main.qml"))); myclass data; viewer.rootContext() ->setContextProperty("myclassData", &data); viewer.setMainQmlFile(QStringLiteral("qml/Versuch2/main.qml")); QObject *viewerobject = viewer.rootObject(); QObject::connect(viewerobject, SIGNAL(qmlSignal(QString)), &data, SLOT(cppSlot(QString))); viewer.showExpanded(); return app.exec();}void myclass::cppSlot(QString msg) { qDebug() <<QString ("Called the cpp slot with message: %1").arg(msg);}Thank You. 解决方案 Using Qt 5.4.0 and Qt Creator 3.3.0, create New Project:Click New ProjectQt Quick ApplicationClick Choose...Name the project and select where to place itClick NextSelect Qt Quick 2.4 from the drop-down menuClick NextSelect desired Kit(s)Click NextClick FinishNow You should see open main.qml file with following code:import QtQuick 2.4import QtQuick.Window 2.2Window { visible: true MainForm { anchors.fill: parent mouseArea.onClicked: { Qt.quit(); } }}Make changes to the file so that it looks like the following:import QtQuick 2.4import QtQuick.Window 2.2Window { visible: true //### New Code ### signal myQmlSignal(string msg) //################ MainForm { anchors.fill: parent mouseArea.onClicked: { //### New Code ### //Replace "Qt.quit();" with console.log("Sending myQmlSignal from QML..."); myQmlSignal("Hello from QML") //################ } }}Add new class to Your project:Right Mouse Click project name in Projects viewerClick Add New...Select C++ Class if not already selectedClick Choose...In Class name filed enter "MyCppClass"Set Base class to QObjectClick NextClick FinishOpen mycppclass.h file, it should look like the following:#ifndef MYCPPCLASS_H#define MYCPPCLASS_H#include <QObject>class MyCppClass : public QObject{ Q_OBJECTpublic: explicit MyCppClass(QObject *parent = 0); ~MyCppClass();signals:public slots:};#endif // MYCPPCLASS_HMake changes to mycppclass.h so it looks like this:#ifndef MYCPPCLASS_H#define MYCPPCLASS_H#include <QObject>//### New Code ####include <QDebug>//################class MyCppClass : public QObject{ Q_OBJECTpublic: explicit MyCppClass(QObject *parent = 0); ~MyCppClass();signals:public slots: //### New Code ### void myCppSlot(const QString &msg); //################};#endif // MYCPPCLASS_HOpen mycppclass.cpp, which should look like this:#include "mycppclass.h"MyCppClass::MyCppClass(QObject *parent) : QObject(parent){}MyCppClass::~MyCppClass(){}Change it to this:#include "mycppclass.h"MyCppClass::MyCppClass(QObject *parent) : QObject(parent){}MyCppClass::~MyCppClass(){}void MyCppClass::myCppSlot(const QString &msg){ //### New Code ### qDebug() << "Signal was received by C++. It contains follwoing message: " << msg; //################}Open main.cpp, which looks like this:#include <QGuiApplication>#include <QQmlApplicationEngine>int main(int argc, char *argv[]){ QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec();}And make following changes:#include <QGuiApplication>#include <QQmlApplicationEngine>//### New Code ####include "mycppclass.h"//################int main(int argc, char *argv[]){ QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); //### New Code ### MyCppClass myCppClass; QObject::connect(engine.rootObjects().takeFirst(), SIGNAL(myQmlSignal(QString)), &myCppClass, SLOT(myCppSlot(QString))); //################ return app.exec();}Click big green triangle to compile and run Your application. Watch Application Output area, click Hello World, You should see following message being printed out:qml: Sending myQmlSignal from QML...Signal was received by C++. It contains follwoing message: "Hello from QML" 这篇关于setMainQmlFile`,rootObject和showExpanded不是QQmlApplicationEngine的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 05-29 02:01