PDA

View Full Version : signals and slots with container types like QHash


themolecule
28th August 2007, 04:47
I am having trouble emit()ing a QHash<QString, QVariant> ...

when I do


QHash<QString, QVariant> payload;
payload["variable"]="abc";

emit(doCommand(payload));


and then


void receiver::doCommand(QHash<QString, QVariant> command)
{
qDebug() << "The variable was: " << command["variable"].toString();
}


I end up with an empty string.

Do I have to register the QHash type, or create a typedef, or is the problem that because "payload" in the first piece of code is not a pointer, and therefore can fall out of scope, it is deleted, or perhaps it cannot be passed by value?

It seems that when I emit a signal with a container (or perhaps just a template), the data gets lost.

themolecule
28th August 2007, 05:48
I now notice QVariantMap, which I will use instead of the QHash<QString, QVariant>, but even after the replacement, the response is the same.

themolecule
28th August 2007, 06:31
It was my mistake all along...

it does pass containers and templates, or at least it passes QVariantMap...

I had a typo in my code... which turned out to be in the stream version....

thanks!

jpn
28th August 2007, 09:07
Adding a Q_DECLARE_METATYPE() (http://doc.trolltech.com/latest/qmetatype.html#Q_DECLARE_METATYPE) makes the type known to all template based functions, including QVariant. Note that if you intend to use the type in queued signal and slot connections, you also have to call qRegisterMetaType() (http://doc.trolltech.com/latest/qmetatype.html#qRegisterMetaType) since such connections are resolved at runtime.