http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#connecting-to-qml-signals
我已阅读该文章的大部分内容,并且似乎理解除了c ++中的部分之外的所有内容。 QObject::connect(item, SIGNAL(qmlSignal(QString)),
&myClass, SLOT(cppSlot(QString)));
很明显item
,SIGNAL
,myClass
,SLOT
,cppSlot
和QString
来自哪里,但{{{} 1}}来自?当然它来自.qml文件,但是如果它是通过运行时加载的,那么编译器如何找到它呢?
答案 0 :(得分:3)
很清楚item,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/