PDA

View Full Version : Using QMetaObject::invokeMethod on a SLOT that has a QString Reference argument?



cloaked_and_coastin
19th January 2012, 17:13
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".

wysota
19th January 2012, 17:20
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?

Spitfire
20th January 2012, 15:22
If you have a slot that accepts a reference like that:

void someSlot( const QString& ref );then you don't use reference when specifying type in the Q_ARG():

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".

wysota
20th January 2012, 16:23
I think the point is he has a non-const reference.