我是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")
}
}
^
您应该显式转换为QObject *
:
QObject *item = qobject_cast< QObject * >( view.rootObject() );
还要确保您已包含QGraphicsObject
:
#include <QGraphicsObject>