Results 1 to 9 of 9

Thread: Save QMap with structure to file.

  1. #1
    Join Date
    Dec 2009
    Posts
    23
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Save QMap with structure to file.

    Hello,
    I am new user Qt and I have got problem. So, I don't know how to save QMap with structure to file. I will show code.

    Qt Code:
    1. void Test::saveMap() {
    2. QString fileName = QFileDialog::getSaveFileName(this, trUtf8("Save map to file"), "", trUtf8("Map (*.ttx);;All files (*)"));
    3. if (fileName.isEmpty())
    4. return;
    5. else {
    6. QFile file(fileName);
    7. if (!file.open(QIODevice::WriteOnly)) {
    8. QMessageBox::information(this, trUtf8("Can't to open file."), file.errorString());
    9. return;
    10. }
    11. QDataStream out(&file);
    12. out.setVersion(QDataStream::Qt_4_6);
    13. out << map;
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. struct DataMap {
    2. QString name;
    3. QString name1;
    4. QString name2;
    5. };
    6.  
    7. ...
    8.  
    9. QMap<QString, DataMap> map;
    To copy to clipboard, switch view to plain text mode 

    QMap is declared as private - it is all ok. If I save QMap without structure (declaration: QMap<QString, QString> map) all is ok, but if I save QMap with structure DataMap, I have got a lot of errors. How does it fix? How to save QMap with structure to file?

    Thanks.

    Goodbye.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Save QMap with structure to file.

    Hi,

    see qRegisterMetaTypeStreamOperators and Q_DECLARE_METATYPE in addition.

  3. #3
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Save QMap with structure to file.

    An alternative way is to use QSettings.

  4. #4
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4

    Default Re: Save QMap with structure to file.

    I have the same problem!!!!!!! I've read the proposed documentation but i always have the error....

    Has anyone solved it?

    Here is my code:

    // Mapping between String and MyStruct
    QMap<QString, MyStruct> mydata;

    -------
    -------
    -------

    QDataStream out(&file);
    out.setVersion(QDataStream::Qt_4_8);
    out << mydata;




    I get this error (also in input mode....):
    /usr/include/qt4/QtCore/qdatastream.h:429: error: no match for 'operator<<' in 'operator<<((* & out), (* & it.QMap<Key, T>::const_iterator::key [with Key = QString, ......

    Can anyone help me?

    Thanks

  5. #5
    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: Save QMap with structure to file.

    Did you implement the << operator for your struct?
    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.


  6. #6
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4

    Default Re: Save QMap with structure to file.

    No, i have not overloaded the << operator...... do you think is it necessary??

    Because i've read this reference (http://qt-project.org/doc/qt-4.8/qsettings.html) but i have not understood the linking with my problem!
    So sorry i'm a beginner

    Best

  7. #7
    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: Save QMap with structure to file.

    Quote Originally Posted by Danilo View Post
    No, i have not overloaded the << operator...... do you think is it necessary??
    Yes, it is necessary.

    And I don't see what QSettings has anything to do with your situation since you are not using it but rather QDataStream.
    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.


  8. #8
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4

    Default Re: Save QMap with structure to file.

    Ok, finally i've understand......
    So, i must overload the operator <<, in this way defined:

    template <class Key, class T>
    Q_OUTOFLINE_TEMPLATE QDataStream &operator<<(QDataStream &out, const QMap<Key, T> &map)
    {
    out << quint32(map.size());
    typename QMap<Key, T>::ConstIterator it = map.end();
    typename QMap<Key, T>::ConstIterator begin = map.begin();
    while (it != begin) {
    --it;
    out << it.key() << it.value();
    }
    return out;
    }


    substituting it with:

    template <class Key, class T>
    Q_OUTOFLINE_TEMPLATE QDataStream &operator<<(QDataStream &out, const QMap<Key, T> &map)
    {
    out << quint32(map.size());
    typename QMap<Key, T>::ConstIterator it = map.end();
    typename QMap<Key, T>::ConstIterator begin = map.begin();
    while (it != begin) {
    --it;
    out << it.key() << it.value().FIELD_1_of_MyStruct << it.value().FIELD_2_of_MyStruct ......
    }
    return out;
    }


    Is it correct?

  9. #9
    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: Save QMap with structure to file.

    You only need

    Qt Code:
    1. QDataStream& operator<<(const MyStruct &);
    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.


Similar Threads

  1. How to save file with QFileDialog
    By pnikolov in forum Qt Programming
    Replies: 11
    Last Post: 1st June 2012, 10:23
  2. Save qpixmap to file
    By been_1990 in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 17:36
  3. Read binary from file
    By weldpua2008 in forum Newbie
    Replies: 2
    Last Post: 3rd April 2009, 23:50
  4. Restrict user from entering space in Save As File Name
    By jyoti kumar in forum Qt Programming
    Replies: 1
    Last Post: 5th September 2007, 12:47
  5. How to check a file for changes since last save
    By nmather in forum General Programming
    Replies: 2
    Last Post: 21st April 2007, 23:43

Tags for this Thread

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.