在qt main.cpp中转换rootobject()以与qml通信时出错

时间:2014-09-19 06:48:51

标签: c++ qt qt-quick

我是qt quick的新手,我试着写一个代码来调用qml信号上的c ++插槽

//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();
}

错误是: main.cpp:15:错误:初始化时无法将'QGraphicsObject *'转换为'QObject *'          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")
    }
}


                                       ^

1 个答案:

答案 0 :(得分:0)

您应该明确地转换为QObject *

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

还要确保包含QGraphicsObject

#include <QGraphicsObject>