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.
Code:
void Test::saveMap() {
QString fileName
= QFileDialog::getSaveFileName(this, trUtf8
("Save map to file"),
"", trUtf8
("Map (*.ttx);;All files (*)"));
if (fileName.isEmpty())
return;
else {
QMessageBox::information(this, trUtf8
("Can't to open file."),
file.
errorString());
return;
}
out << map;
}
}
Code:
struct DataMap {
};
...
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.
Re: Save QMap with structure to file.
Hi,
see qRegisterMetaTypeStreamOperators and Q_DECLARE_METATYPE in addition.
Re: Save QMap with structure to file.
An alternative way is to use QSettings.
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
Re: Save QMap with structure to file.
Did you implement the << operator for your struct?
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
Re: Save QMap with structure to file.
Quote:
Originally Posted by
Danilo
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.
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?
Re: Save QMap with structure to file.
You only need
Code:
QDataStream& operator<<(const MyStruct &);