Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't register C++ type to QML system

I'm trying to add C++ type to 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'm trying compile this(i don't import this in .qml file, only testing) I'm geting errors about debugging.

enter image description here

What's wrong?

Thanks.

like image 253
Sekhmet Avatar asked Oct 04 '22 11:10

Sekhmet


1 Answers

You're mixing Qt Quick 1 and 2, which is unsupported. The QDeclarative headers are for Quick 1 and the QQml headers are for Quick 2.

Your includes 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 there for moc to work its magic.

For more details, see this already reported bug in Qt:

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

like image 80
Alex Reinking Avatar answered Oct 19 '22 05:10

Alex Reinking