Cannot register C ++ type for QML system

I am trying to add a C ++ type to a QML system.

#include <QtGui/QGuiApplication> #include <QDeclarativeEngine> #include <QDeclarativeComponent> #include "qtquick2applicationviewer.h" #include <QQmlApplicationEngine> class FooBar: public QObject { Q_OBJECT }; int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine("qml/RBot/main.qml"); qmlRegisterType<FooBar>("io.secorp", 1, 0, "FooBar"); return app.exec(); } 

But when I try to compile this (I do not import it into a .qml file, just testing), I get debugging errors.

enter image description here

What's wrong?

Thanks.

+4
source share
1 answer

You are mixing Qt Quick 1 and 2, which is not supported. QDeclarative headers are for Quick 1, and QQml headers are for Quick 2.

Your inclusions should be:

 #include <QtGui/QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlComponent> #include "qtquick2applicationviewer.h" #include "foobar.h" 

The definition of FooBar should be in its own header, and QObject should be included where moc will work with its magic.

More details about this have already been reported in Qt:

QTBUG-32138 - Hello World for QtQuick2 does not compile C # include when QML debugging is enabled

+4
source

Source: https://habr.com/ru/post/1493008/


All Articles