简体   繁体   中英

error in casting rootobject() in qt main.cpp for communicating with qml

I am new to qt quick ,i tried to write a code to call c++ slot on qml signal

//main.cpp 
#include "qtquick1applicationviewer.h"
#include "QApplication"
#include"authenticate.h"

     int main(int argc, char *argv[])
     {
      QApplication app(argc, argv);
      QDeclarativeView view(QUrl::fromLocalFile("MyItem.qml"));
      QObject *item = view.rootObject();

      Authenticate myClass;
      QObject::connect(item, SIGNAL(qmlSignal(QString)),
                     &myClass, SLOT(cppSlot(QString)));

     view.show();

    return app.exec();
}

error is: main.cpp:15: error: cannot convert 'QGraphicsObject*' to 'QObject*' in initialization QObject *item = viewer.rootObject();

     //main.qml
     import QtQuick 1.0 

    Item {
    id: item
    width: 100; height: 100

    signal qmlSignal(String msg)

    MouseArea {
        anchors.fill: parent
        onClicked: item.qmlSignal("Hello from QML")
    }
}


                                       ^

You should explicitly cast to QObject * :

QObject *item = qobject_cast< QObject * >( view.rootObject() );

Also make sure you have included QGraphicsObject :

#include <QGraphicsObject>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM