Connecting to slot by string name
-
result = connect(this, SIGNAL(testSignal()), this, SLOT(testSlot()));
replace the above line and check.
Thanks,
-
@poor_robert said in Connecting to slot by string name:
of course you're right, but in this case I want to know how to connect with slot name in parenthesis so SLOT("testSlot()")); is exactly what I want to try.
Of course I may be wrong and I would welcome an opinion from someone more advanced in the subject but according to my knowledge this will not work at all.
-
@poor_robert said in Connecting to slot by string name:
I want to know how to connect with slot name in parenthesis so SLOT("testSlot()"));
Why? Why do you want to use "?
-
-
The code given above was just an example. The real reason was to use slot as callback. I have created following method and wanted to do following connection:
void MyClass::setCallback(QObject * obj, const char* slotName) { connect(m_member, SIGNAL(updated()), obj, SLOT(slotName)); }
in my implementation obj is class derived from the QObject. I have solved the problem with help of std::function used as callbacks.
Do you know how to solve this in Qt way? -
-
@poor_robert said in Connecting to slot by string name:
Just use the SLOT at the place where you call the function:
void MyClass::setCallback(QObject * obj, const char* slotName) { connect(m_member, SIGNAL(updated()), obj, slotName); } setCallback(myObj,SLOT(myFunction()));
-Michael.
-
@poor_robert Just pass SLOT(mySlot()) to setCallback :-)
void MyClass::setCallback(QObject * obj, const char* slotStr) { connect(m_member, SIGNAL(updated()), obj, slotStr); } setCallback(obj, SLOT(mySlot()));