qt5 qml c++ interaction

qt5 qml c++ interaction

本文关键字:interaction c++ qml qt5      更新时间:2023-10-16

http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#connecting-至qml信号

我已经阅读了那篇文章的大部分内容,除了c++中的部分外,似乎什么都懂。QObject::connect(item, SIGNAL(qmlSignal(QString)), &myClass, SLOT(cppSlot(QString)));

很清楚itemSIGNALmyClassSLOTcppSlotQString来自哪里,但qmlSignal来自哪里?当然,它来自.qml文件,但是如果它是通过运行时加载的,编译器如何发现它呢?

项目、SIGNAL、myClass、SLOT、cppSlot和QString的位置很清楚来自,但qmlSignal来自哪里?当然它来自qml文件,但是如果它是通过运行时加载的?

qmlSignal由QML代码发出,并在C++端捕获,如您所述。编译器对运行时发生的事情一无所知,只知道代码处理的C++类型。

根QML项反映到QObject,它有一个嵌套的信号和槽列表,非常像纯C++QObject。每个信号和槽都有一个测试字符串签名,槽也有到特定类成员的映射。

QQuickView view(QUrl::fromLocalFile("MyItem.qml"));
QObject *item = view.rootObject(); // get QObject from QML root
MyClass myClass;
QObject::connect(item, SIGNAL(qmlSignal(QString)), // find "qmlSignal(QString)" in the list of signals of 'item'
                 &myClass, SLOT(cppSlot(QString))); // connect that signal entry to the found cppSlot(QString) entry of myClass object

为了更好地理解信号槽内部,有一篇好文章:http://woboq.com/blog/how-qt-signals-slots-work.html

以上当然是关于基于字符串的连接:http://doc.qt.io/qt-5/signalsandslots-syntaxes.html

关于QML信号/插槽绑定的文章不多,但有一些:http://www.kdab.com/qml-engine-internals-part-2-bindings/