Results 1 to 4 of 4

Thread: signals and slots with container types like QHash

  1. #1
    Join Date
    Jul 2007
    Location
    New York
    Posts
    45
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default signals and slots with container types like QHash

    I am having trouble emit()ing a QHash<QString, QVariant> ...

    when I do

    Qt Code:
    1. QHash<QString, QVariant> payload;
    2. payload["variable"]="abc";
    3.  
    4. emit(doCommand(payload));
    To copy to clipboard, switch view to plain text mode 

    and then

    Qt Code:
    1. void receiver::doCommand(QHash<QString, QVariant> command)
    2. {
    3. qDebug() << "The variable was: " << command["variable"].toString();
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jul 2007
    Location
    New York
    Posts
    45
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

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

  3. #3
    Join Date
    Jul 2007
    Location
    New York
    Posts
    45
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default 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!

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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.
    J-P Nurmi

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.