Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot convert QQuickItem to QObject
Qt 6.11 is out! See what's new in the release blog

Cannot convert QQuickItem to QObject

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 16.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SteveG
    wrote on last edited by A Former User
    #1

    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?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

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

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SteveG
        wrote on last edited by
        #3

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

        veenusavV 1 Reply Last reply
        4
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          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>
          @

          (Z(:^

          1 Reply Last reply
          2
          • S Offline
            S Offline
            SteveG
            wrote on last edited by
            #5

            That works! Thanks again.

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

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              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.

              (Z(:^

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                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 .

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SteveG
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • S SteveG

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

                        veenusavV Offline
                        veenusavV Offline
                        veenusav
                        wrote on last edited by
                        #11

                        @SteveG It worked for me too. thank you

                        1 Reply Last reply
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved