[Solved] Cannot convert QQuickItem to QObject



  • I am using Qt 5.2 beta 1 on windows 8 and have encountered a problem. I followed the example "Interacting with QML Objects from C++":http://qt-project.org/doc/qt-5.1/qtqml/qtqml-cppintegration-interactqmlfromcpp.html
    @
    // Using QQuickView
    QQuickView view;
    view.setSource(QUrl::fromLocalFile("MyItem.qml"));
    view.show();
    QObject *object = view.rootObject(); // <== GENERATES ERROR
    @

    but I get the error message "cannot convert 'QQuickItem' to 'QObject' in initialization" for the last line. Any suggestions?


  • Moderators

    Do a qobject_cast, or use QQuickItem pointer instead of QObject.



  • Thanks. It works when I include <QtQuick> instead of <QtQuick/QQuickView>.


  • Moderators

    You loose compile time by doing that. Since you are now certain the issue is with headers, try to include hard dependencies only:
    @
    #include <QQuickItem>
    #include <QQuickView>
    @



  • That works! Thanks again.

    BTW, Does it matter if I use <QQuickItem> or <QtQuick/QQuickItem>, etc? (I suspect not.)


  • Moderators

    No, not really. Just <QQuickItem> is considered more "portable": if the class is moved to another module in the future (Qt6...) it will still compile. But that is such a minor thing that I won't bother much. It was very useful in Qt4->Qt5 migration, though.



  • Hi guys, thanks SteveG for this post.
    In my case with the same code I obtain those errors:

    • " warning: C4189: 'object' : local variable is initialized but not referenced"

    • "main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall QQuickView::~QQuickView(void)" (_imp??1QQuickView@@UAE@XZ) referenced in function _main"

    • "main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: class QQuickItem * __thiscall QQuickView::rootObject(void)const " (_imp?rootObject@QQuickView@@QBEPAVQQuickItem@@XZ) referenced in function _main"

    • "main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall QQuickView::setSource(class QUrl const &)" (_imp?setSource@QQuickView@@QAEXABVQUrl@@@Z) referenced in function _main"

    • "main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QQuickView::QQuickView(class QWindow *)" (_imp??0QQuickView@@QAE@PAVQWindow@@@Z) referenced in function _main"

    • "error: LNK1120: 4 unresolved externals"

    When I use the "qobject_cast" i've got this error: "error: C2065: 'object' : undeclared identifier".

    I don't get what's wrong.

    NB: I am working on Qt 5.1.1 .



  • Woow! After a "Run qmake" and a "Build All" I don't have anymore errors or warnings, it compiles and works.



  • Maybe the ghosts and goblins are working overtime for Halloween. ;-)



  • :) Yeah, next time I'll contact directly the ghostbusters instead to post here.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.