Thanks, QMetaObject::InvokeMethod works better than signals/slot for C++ invoking QML. From same document, QML invoking C++, i could not compile, below is my code. Can help?
-----------------cpp file----------------
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
class MyClass: public QObject
{
Q_OBJECT
public slots:
void cppSlot(const QString &msg){
qDebug()<<"Called the c++ slot with message"<<msg;
}
};
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView view("qrc:///main.qml");
QObject *item= view.rootObject();
MyClass myClass;
QObject::connect(item,SIGNAL(qmlSignal(QString)),&myClass,SLOT(cppSlot(QString)));
return app.exec();
}
-----------------qml file----------------
import QtQuick 2.2
import QtQuick.Controls 1.1
ApplicationWindow {
id: item
visible: true
width: 640
height: 480
title: qsTr("Hello World")
signal qmlSignal(string msg)
MouseArea{
anchors.fill:parent
onClicked: item.qmlSignal("Hello from QML")
}
}
↧