signals and slots with container types like QHash
I am having trouble emit()ing a QHash<QString, QVariant> ...
when I do
Code:
QHash<QString, QVariant> payload;
payload["variable"]="abc";
emit(doCommand(payload));
and then
Code:
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.
Re: signals and slots with container types like QHash
I now notice QVariantMap, which I will use instead of the QHash<QString, QVariant>, but even after the replacement, the response is the same.
Re: signals and slots with container types like QHash
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!
Re: signals and slots with container types like QHash
Adding a 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() since such connections are resolved at runtime.