Can the signal slot connection method in Cpp && Qml be used in Qt5?
-
Hi, in the connection of signals and slots in Cpp && Qml (Qml emits signals and connects to the slot function of a class in C++), I found many ways to do this: QObject:connect(item, SIGNAL(.. ), &MyClass, SLOT(...)), many books have this kind of Qt4 syntax. Can't I use Qt5's new syntax?
-
Hi, in the connection of signals and slots in Cpp && Qml (Qml emits signals and connects to the slot function of a class in C++), I found many ways to do this: QObject:connect(item, SIGNAL(.. ), &MyClass, SLOT(...)), many books have this kind of Qt4 syntax. Can't I use Qt5's new syntax?
@Nan-Feng said in Can the signal slot connection method in Cpp && Qml be used in Qt5?:
Can't I use Qt5's new syntax?
Unfortunately not. Qt 5's new syntax only works because the C++ compiler can check both the signal and the slot. However, the C++ compiler cannot see QML signals or QML slots.
The old (Qt 4) works here because the checks are done at runtime, using string comparisons.
-
Note that doing signal slot connection between c++ and QML objects in c++ is most of the time a bad practice.
Some links explaining why:
-
Note that doing signal slot connection between c++ and QML objects in c++ is most of the time a bad practice.
Some links explaining why:
@GrecKo said in Can the signal slot connection method in Cpp && Qml be used in Qt5?:
Note that doing signal slot connection between c++ and QML objects in c++ is most of the time a bad practice.
Some links explaining why:
The links encourage developers to avoid calling QML object methods directly from C++. Using signal-slot connections are quite different from the calling methods directly, since they still allow good decoupling. The same idea applies to pure C++ code -- If the sender object does not know anything about the receiver object, then decoupling has been achieved.
-
@GrecKo said in Can the signal slot connection method in Cpp && Qml be used in Qt5?:
Note that doing signal slot connection between c++ and QML objects in c++ is most of the time a bad practice.
Some links explaining why:
The links encourage developers to avoid calling QML object methods directly from C++. Using signal-slot connections are quite different from the calling methods directly, since they still allow good decoupling. The same idea applies to pure C++ code -- If the sender object does not know anything about the receiver object, then decoupling has been achieved.
@JKSH No it doesn't really allow good decoupling, to connect to a QML object from C++, you have to be aware of it.
The example in the first link is specifically about connecting a QML signal to a c++ slot.
If your C++ object is a helper for QML to which you pass the QML object reference from QML and is not related to your business logic it's fine.
Using
findChild
or traversing the QML object hierarchy from c++ is almost always not fine.