Solved Emit a signal with QML Items QObject * as argument
-
Hi,
I have a multi threaded program, and would like to make an update mechanism for parameters which are in the backend but are displayed in the user interface.
For this I thought I would make a ParameterManager in der GUI thread, and this should receive signals e.g. form CheckBoxes in the GUI. For this it would be good, if the signal coming from my QtComponents would allow to identify which ones they are (I thought about storing a map of those objects (QObject *) and their corresponding parameters in the ParameterManager.
So In cpp I could just emit a signal with
this
as Parameter. How can this be done in qml? -
@maxwell31 said in Emit a signal with QML Items QObject * as argument:
So In cpp I could just emit a signal with this as Parameter. How can this be done in qml?
Item{ signal sig(var o) Component.onCompleted : sig(this) onSig : console.log(o) }
-
So this
this
is the same as the pointer I get when I use findChild(objectName)? -
you can test it
@maxwell31 said in Emit a signal with QML Items QObject * as argument:So this this is the same as the pointer I get when I use findChild(objectName)?
ApplicationWindow { width: 800 height: 600 visible: true Item{ objectName: "obj" signal sig(var o) Component.onCompleted : sig(this) onSig : utils.test(o) } }
//a class Q_INVOKABLE void test(QQuickItem * rect){ qDebug() << rect; }
//main.cpp QList<QObject*> objects = engine.rootObjects(); qDebug()<<objects.first()->children().at(1);
see the warning at the end
https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c -
Thanks! I used your code, and want to extend it to use
QObject::connect
like this:QObject * o = engine.rootObjects().value(0)->findChild<QObject*>("obj"); CatchSignal s; QObject::connect(o, SIGNAL(sig(QObject *)),&s,SLOT(catchsignal(QObject*)));
but get the error message:
QObject::connect: No such signal QQuickItem_QML_0::sig(QObject *) in /home/maxwell/work/quicktest2/qobjecttest/main.cpp:24 QObject::connect: (sender name: 'obj')
Do you know how one has to use it in connect?
-
Ok, I got it. Obviously the slot and signal need to have QVariant as argument. Thanks!
-
you can do
signal sig(QtObject o)
.But don't connect to a QML signal from C++.
Just call a C++ Q_INVOKABLE/Slots from QML. -
I didn't know that there is also the sender() function which you can use in a slot, therefore eliminating the need to pass an object.
What are the reasons you suggest to call a Q_INVOKABLE instead of connecting in C++?