PDA

View Full Version : Save QMap with structure to file.



Xandareva
19th December 2009, 09:29
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.



void Test::saveMap() {
QString fileName = QFileDialog::getSaveFileName(this, trUtf8("Save map to file"), "", trUtf8("Map (*.ttx);;All files (*)"));
if (fileName.isEmpty())
return;
else {
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::information(this, trUtf8("Can't to open file."), file.errorString());
return;
}
QDataStream out(&file);
out.setVersion(QDataStream::Qt_4_6);
out << map;
}
}




struct DataMap {
QString name;
QString name1;
QString name2;
};

...

QMap<QString, DataMap> map;


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.

Lykurg
19th December 2009, 09:33
Hi,

see qRegisterMetaTypeStreamOperators and Q_DECLARE_METATYPE in addition.

Tanuki-no Torigava
20th December 2009, 22:19
An alternative way is to use QSettings.

Danilo
5th March 2013, 15:04
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

wysota
5th March 2013, 15:42
Did you implement the << operator for your struct?

Danilo
5th March 2013, 16:01
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

wysota
5th March 2013, 16:13
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.

Danilo
5th March 2013, 16:20
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?

wysota
5th March 2013, 21:41
You only need


QDataStream& operator<<(const MyStruct &);