Using QMetaObject::invokeMethod on a SLOT that has a QString Reference argument?
I've looked all over the place and tried many different ways to call a slot with QMetaObject::invokeMethod whose only argument is a QString&. Here is my code:
QString myString = QString(somePoundDefine);
QMetaObject::invokeMethod(this, "mySlot", Qt::QueuedConnection, Q_ARG(QString&, myString)); //Note that there is NO space between the "QString" and the "&" in the Q_ARG call.
When I run the above code, I get the following printout:
QMetaMethod::invoke: Unable to handle unregistered datatype 'QString&'
Which makes me go to:
http://doc.qt.nokia.com/4.7-snapshot/qmetamethod.html
to read the documentation on that error which says to use qRegisterMetaType().
I've tried a couple different ways of trying to register a QString& data type with no luck.
This may be a case of not seeing the forest for the trees.
Help.
Note that this was originally posted on the "Qt-based Software" part of the forum. Re-posted here under a slightly different title because it seemed more relevant under "Qt Programming".
Re: Using QMetaObject::invokeMethod on a SLOT that has a QString Reference argument?
You can't queue references. The argument gets copied when queued so all references would be broken. What is it that you are trying to achieve?
Re: Using QMetaObject::invokeMethod on a SLOT that has a QString Reference argument?
If you have a slot that accepts a reference like that:
Code:
void someSlot( const QString& ref );
then you don't use reference when specifying type in the Q_ARG():
Code:
QMetaObject::invokeMethod( this,
"someSlot", Qt
::QueuedConnection, Q_ARG
(QString, someString
) );
The important bit is the const in slot definition.
As wysota mentioned it's a copy of a string so there's no point in changing it.
Also when you register new type you don't register "SomeType&", just "SomeType".
Re: Using QMetaObject::invokeMethod on a SLOT that has a QString Reference argument?
I think the point is he has a non-const reference.