Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Cannot execute examples from Doc



  • Hello,

    I'm trying to learn about connections between QML and C++ and I don't understand why I cannot get the examples from doc working.

    For example, I try to use this simple example.

    So my full main.cpp code is:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQuickView>
    
    class MyClass : public QObject
    {
        Q_OBJECT
    public slots:
        void cppSlot(const QString &msg) {
            qDebug() << "Called the C++ slot with message:" << msg;
        }
    };
    
    int main(int argc, char *argv[]) {
        QGuiApplication app(argc, argv);
    
        QQuickView view(QUrl::fromLocalFile("MyItem.qml"));
        QObject *item = view.rootObject();
    
        MyClass myClass;
        QObject::connect(item, SIGNAL(qmlSignal(QString)),
                         &myClass, SLOT(cppSlot(QString)));
    
        view.show();
        return app.exec();
    }
    
    

    And when I try to build it, I get the following errors:

    main.cpp:18: error: cannot initialize a variable of type 'QObject *' with an rvalue of type 'QQuickItem *'
    main.cpp:-1: In function 'int qMain(int, char**)':
    main.cpp:18: error: cannot convert 'QQuickItem*' to 'QObject*' in initialization
         QObject *item = view.rootObject();
                                         ^
    D:\Qt\5.12.6\mingw73_64\include\QtQuick\qquickview.h:43: In file included from D:\Qt\5.12.6\mingw73_64\include\QtQuick/qquickview.h:43:0,
    D:\Qt\5.12.6\mingw73_64\include\QtQuick\QQuickView:1: from D:\Qt\5.12.6\mingw73_64\include\QtQuick/QQuickView:1,
    main.cpp:3: from ..\test2\main.cpp:3:
    D:\Qt\5.12.6\mingw73_64\include\QtQuick\qquickwindow.h:55: class type 'QQuickItem' is incomplete
     class QQuickItem;
           ^~~~~~~~~~
    

    How to solve it and why is it so complex to use examples?

    Regards


  • Lifetime Qt Champion

    Hi @AlainBo

    please show your project .pro file.

    Regards



  • @AlainBo said in Cannot execute examples from Doc:

    'QQuickItem' is incomplete

    include QQuickItem



  • @LeLev That fixes it, thank you.
    The creator should set this thread as resolved....


Log in to reply