Results 1 to 4 of 4

Thread: Store QList<T> in QVariant and stream to QDataStream?

  1. #1
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Store QList<T> in QVariant and stream to QDataStream?

    Hi,
    I'm trying to do what the title suggests...

    QList<Custom> L;
    QVariant v(QVariant::fromValue(l));
    QDataStream d;
    d << v;

    The problem seems to be that d doesn't know how to stream v, because v doesn't know how to do a metatype save on L. I have registered Custom and L as metatypes and I've also registered their IO streams, but L has no meta object, and I think that is the problem.

    Can I get around this somehow?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Store QList<T> in QVariant and stream to QDataStream?

    There is no free beer here You have to implement proper streaming operators yourself.

    Use this:
    Qt Code:
    1. void qRegisterMetaTypeStreamOperators ( const char * typeName )
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Store QList<T> in QVariant and stream to QDataStream?

    I did register them, but it doesn't work. The specific error that I get at runtime is "ASSERT failure in QVariant::save: "Invalid type to save", file kernel\qvariant.cpp, line 1952".

    Later edit: the problem was that when calling qRegisterMetaTypeStreamOperators<CustomList>("clis t"), "clist" should have been "CustomList", the string matters!
    Last edited by razvan.petru; 18th September 2009 at 09:04.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Store QList<T> in QVariant and stream to QDataStream?

    Sure it matters It has to be identical to what's returned by the metatype.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    razvan.petru (18th September 2009)

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.