Results 1 to 3 of 3

Thread: Serialize and read a QVector<QMultiMap<QString, QString>> to a QDataStream

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    672
    Thanks
    55
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Serialize and read a QVector<QMultiMap<QString, QString>> to a QDataStream

    Hi community,
    I need to serialize a vector of multi maps to a data stream and read it back.

    I did this to write it:

    Qt Code:
    1. // Retrieve the model from mainwindow
    2. SelectedListModel *model = reinterpret_cast<SelectedListModel*>(ui->selectedItemsView->model());
    3. // Get the reports data
    4. QVector<QMultiMap<QString, QString>> reports = model->reports();
    5.  
    6. QFile fileData(m_configPath + QDir::separator() + "file.dat");
    7. fileData.open(QIODevice::WriteOnly);
    8. QDataStream outData(&fileData);
    9. for (int idx = 0; idx < reports.size(); idx++)
    10. {
    11. // ... more code ...
    12. outData << reports[idx];
    13. }
    14.  
    15. fileData.flush();
    16. fileData.close();
    To copy to clipboard, switch view to plain text mode 

    Now I have some problem in reading it back from the file.
    When I try to read it, it does not read the full data.

    Qt Code:
    1. QFile fileData(m_configPath + QDir::separator() + "file.dat");
    2. fileData.open(QIODevice::ReadOnly);
    3.  
    4. QDataStream in(&fileData);
    5. QMultiMap<QString, QString> mmap;
    6. in >> mmap;
    7.  
    8. qDebug() << mmap;
    To copy to clipboard, switch view to plain text mode 

    Can I have a help?
    Regards
    Franco Amato

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,121
    Thanks
    291
    Thanked 847 Times in 834 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Serialize and read a QVector<QMultiMap<QString, QString>> to a QDataStream

    Can I have a help?
    You are not performing reciprocal operations when saving and restoring the multimap. If you want to read it using in >> map, then you must save it using out << map. The inherited QMap operator>>() and operator<<() methods will take care of the serialization parts.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    franco.amato (25th January 2022)

  4. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    672
    Thanks
    55
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serialize and read a QVector<QMultiMap<QString, QString>> to a QDataStream

    You always save my life
    Franco Amato

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

    d_stranz (26th January 2022)

Similar Threads

  1. Is it possible to serialize QVariant into a QString?
    By MachinTrucChose in forum Qt Programming
    Replies: 3
    Last Post: 27th October 2012, 22:08
  2. Replies: 2
    Last Post: 11th August 2011, 15:42
  3. QMultiMap And QDataStream
    By amiref in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2011, 10:50
  4. Replies: 0
    Last Post: 25th June 2009, 08:17
  5. QDataStream and QHash<QString,QVariant> crash on read
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 15th July 2008, 12:14

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.