Using QMetaObject::connectSlotsByName both ways
-
Hi,
I have both a C++ class (derived from QObject) and a QQuickItem, which represents a QML file.
Both have slots and signals which need to be interconnected.
Doing this manually (using QObject::connect) works fine.I'd like to use QMetaObject::connectSlotsByName for this, for several reasons, most of all convenience.
Both objects have a different objectName set.@void Backend::doStuff(QQuickItem* item)
{
item->setParent(this); // crucial
QMetaObject::connectSlotsByName(this);
}
@This will connect all signals from the QQuickItem to the relevant slots in the Backend object.
For example if the QML file has a signal called test(), it will automatically connect to Backend::on_Frontend_test() (given that the QQuickItem has the objectName FrontEnd)Thing is, it needs to happen the other way around too - I'd like to have every signal from Backend connected to the Frontend slots the same way.
I've created a signal in the Backend class (let's call it whatever()) and implemented a function called
on_Backend_whatever() in the qml file, but this function is not called when the signal is emitted.I'm guessing the parent>child relationship is the problem here and I'm not sure how to handle it.
Just for laughs, I've tried
@void Backend::doStuff(QQuickItem* item)
{
this->setParent(item);
QMetaObject::connectSlotsByName(item);
item->setParent(this); // crucial
QMetaObject::connectSlotsByName(this);
}
@This compiles, but makes my application crash when the method is called.
So how to do it?
Regards