Convert QQuickItem* to QObject*

  • I have succeed to create some signal an qml and some slot to deal with them.
    I have created an empty project with the new application and it works, but when i add the code to an older app it say
    cannot convert 'QQuickItem*' to 'QObject*' in initialization QObject *item = view2.rootObject()

    My function look like this:

    void fromQML::loadQuestion()
        view2.setSource (QUrl("qrc:/question.qml"));
        QObject *item = view2.rootObject(); //here is the error and doesn't work if i make cast
        loadProcess load;
        QObject::connect(item, SIGNAL(qmlYesSig()),&load, SLOT(forYes()));
        QObject::connect(item, SIGNAL(qmlNoSig()),&load, SLOT(forNo()));

    It the working application the code looks like this:

        QGuiApplication app(argc, argv);
        QQuickView view;
        view.setSource (QUrl("qrc:/main.qml"));
        QObject *item = view.rootObject();
        testClass myClass;
        QObject::connect(item, SIGNAL(qmlYesSig(QString)),
                         &myClass, SLOT(cppSlot(QString)));
        QObject::connect(item, SIGNAL(qmlNoSig(QString)),
                         &myClass, SLOT(cppSlot(QString)));

    From my point of view is the same staff so i don't understand why in first sample doesn't work and is the second does.

  • I have try again and if i make the connection in main it works but if i copy and paste the some code in a C++ slot it give me the above error.

  • Lifetime Qt Champion


    While that is surprising, why not use QQuickItem* item = view2.rootObject(); since it's what the method returns ?

    Note that your load object will be destroyed at the end of loadQuestion so it might not do anything.

Log in to reply

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