Stuck with Qt5.4 project.
-
Hello guys,
I was trying to follow some guys tutorial from the web on integrating C++ and QML, did everything what he/she did but failed with 4 errors kinda the same sort. One of them says: " etMainQmlFile is not a member of QQmlApplicationEngine". Same for the others, rootObject is not a member of QQmlApplicationEngine and showExpanded is not.... . Any ideas on how to solve this? THank you.
Here's the code. (main.cpp)
@#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);
}
@ -
I do not know enough about QML, but probably you have followed a tutorial for Qt 4 which is mainly, but not completely compatible with Qt5.
You can download and install the older version of "Qt4.8":http://download.qt-project.org/archive/qt/ if you like to follow the tutorial. -
-
Hi,
The error messages are correct. QQmlApplicationEngine does not have any member functions called setMainQmlFile(). See http://doc.qt.io/qt-5/qqmlapplicationengine.html for a list of members that it actually has.
I'm guessing that your tutorial used a custom class called QmlApplicationViewer or QtQuick2ApplicationViewer (not QQmlApplicationEngine!). These classes aren't actually part of the Qt API. You see them around because Qt Creator used to generate these custom classes for convenience. However, newer versions of Qt Creator use built-in Qt classes instead.
I suggest you follow a different tutorial, like http://doc.qt.io/qt-5/qml-tutorial.html