我按照一些家伙的教程编写了这段代码,但我无法让它运行。错误说:
setMainQmlFile` `、rootObject和showExpanded不是QQmlApplicationEngine的成员
它应该做的是从QML获取一个信号并打印一条消息(在控制台中)。基本上,我正在尝试集成C++和QML。
编辑
我试图用一些似乎合适的函数来替换一些函数(至少对我来说是这样)。我还试图找到包含什么,以便这些功能可以工作,但没有运气。
#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);
}谢谢。
发布于 2014-12-14 21:51:17
我不知道您在哪里找到了教程,但是关于Qt文档,没有像setMainQmlFile或showExpanded for QQmlApplicationEngine这样的方法。
对于setMainQmlFile(...),尝试使用setSource(...)代替。
对于showExpanded(),它是一个QWidget函数,QQmlApplicationEngine不继承QWidget。
关于rootObject(),它可能是一个错误,您可以使用rootObjects()返回一个QList<QObject*>。
编辑:看来您必须使用Qt的QtQuick2Application向导来重新创建教程中使用的QtQuick2ApplicationViewer类。
发布于 2015-01-05 20:26:57
使用Qt5.4.0和3.3.0,创建新项目:
现在您应该看到使用以下代码打开的main.qml文件:
import QtQuick 2.4
import QtQuick.Window 2.2
Window {
visible: true
MainForm {
anchors.fill: parent
mouseArea.onClicked: {
Qt.quit();
}
}
}对该文件进行更改,使其看起来如下所示:
import QtQuick 2.4
import QtQuick.Window 2.2
Window {
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")
//################
}
}
}向项目中添加新类:
打开mycppclass.h文件,它应该如下所示:
#ifndef MYCPPCLASS_H
#define MYCPPCLASS_H
#include <QObject>
class MyCppClass : public QObject
{
Q_OBJECT
public:
explicit MyCppClass(QObject *parent = 0);
~MyCppClass();
signals:
public slots:
};
#endif // MYCPPCLASS_H对mycppclass.h进行修改,如下所示:
#ifndef MYCPPCLASS_H
#define MYCPPCLASS_H
#include <QObject>
//### New Code ###
#include <QDebug>
//################
class MyCppClass : public QObject
{
Q_OBJECT
public:
explicit MyCppClass(QObject *parent = 0);
~MyCppClass();
signals:
public slots:
//### New Code ###
void myCppSlot(const QString &msg);
//################
};
#endif // MYCPPCLASS_H打开mycppclass.cpp,它应该如下所示:
#include "mycppclass.h"
MyCppClass::MyCppClass(QObject *parent) : QObject(parent)
{
}
MyCppClass::~MyCppClass()
{
}将其更改为:
#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;
//################
}打开main.cpp,它如下所示:
#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();
}并作出以下修改:
#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();
}单击大绿色三角形编译并运行应用程序。查看应用程序输出区域,单击Hello,您将看到以下消息正在打印出来:
从QML发送myQmlSignal .
C++接收信号。它包含如下信息:“来自QML的你好”。
https://stackoverflow.com/questions/27473173
复制相似问题