简体   繁体   中英

qt5 qml c++ interaction

http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#connecting-to-qml-signals

I've read most of that article, and seem to understand everything except for the part in c++. QObject::connect(item, SIGNAL(qmlSignal(QString)), &myClass, SLOT(cppSlot(QString)));

It is clear where item , SIGNAL , myClass , SLOT , cppSlot and QString come from, but where does qmlSignal come from? Of course it comes from the .qml file, but then how does the compiler find out about it if it's loaded via runtime?

It is clear where item, SIGNAL, myClass, SLOT, cppSlot and QString come from, but where does qmlSignal come from? Of course it comes from the .qml file, but then how does the compiler find out about it if it's loaded via runtime?

qmlSignal is emitted by QML code and caught on C++ side as you noted. And compiler knows nothing of what is happening at run time except C++ types the code deals with.

The root QML item is reflected to QObject that has a nested list of signals and slots very much like pure C++ QObject. Every signal and slot has a test string signature and slots also have the mapping to certain class member.

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

For better understanding of signal-slot internals there is a good article: http://woboq.com/blog/how-qt-signals-slots-work.html

The above is of course is about string-based connections: http://doc.qt.io/qt-5/signalsandslots-syntaxes.html

Not so many articles on QML signal/slot bindings but some: http://www.kdab.com/qml-engine-internals-part-2-bindings/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM