How to write and read Qbject in file
-
Hey I am trying to read and write objects in files
I know that we can't use QTextStream because it takes input/output text as text.
So Can anyone suggest me any other way?
Note: If you use QTextStream it will show an invalid operand because it only takes text as an argument and you are passing the wrong operand as arguments
Thanks in advance -
Hi
https://doc.qt.io/qt-5/qdatastream.html
is your friend.It can already stream most Qt types.
Is it your own custom class you want to save and load ?
-
Hi,
QTextStream does not take only text as input. If you want to serialize objects you have to provide the appropriate methods and in this case these are the QTextStream operators.
-
Hi
very fast sampleclass MyClass { int number; QString name; friend QDataStream &operator<<(QDataStream &out, const MyClass &rhs); friend QDataStream &operator>>(QDataStream &in, MyClass &rhs); }; QDataStream &operator<<(QDataStream &out, const MyClass &rhs) { out << rhs.number << rhs.name; return out; } QDataStream &operator>>(QDataStream &in, MyClass &rhs) { in >> rhs.number >> rhs.name; return in; }
void save() { QFile file("file.xxx"); file.open(QIODevice::WriteOnly); QDataStream out(&file); MyClass test; out << test; }
void load() { QFile file("file.xxx"); file.open(QIODevice::ReadOnly); // check foir open errors! QDataStream in(&file); MyClass test; in >> test; }
Do note for a real app, you should include error handling
-
@UG-SEP
Hi
yeah its operator overloading.
the >> and << are operators and we "teach" them how to stream your classes (its members.)The neat part is that most of the Qt types already have such "overloads" and
hence you can save QString and QPixmap and all such classes by simply doing
<< thevar;so in most cases you only need to make sure to stream in and out in the same order as else
it will explode :) -
@mrjj I did understand why It's showing multiple definition errors because we overload that operator as a friend function
My class Sample:class Player : public QDialog { QString name,password; int gp,gw,gl; float wp; public: Player(QString name,QString password) { this->name=name; this->password=password; gp=gw=gl=wp=0; } Player(){}; int operator==(Player &other) { if(name==other.name && password==other.password) return 1; else return 0; } friend QDataStream &operator<<(QDataStream &out, const Player &new_player); friend QDataStream &operator>>(QDataStream &in,Player &existing_player); }; QDataStream &operator<<(QDataStream &out, const Player &new_player) { out << new_player.name << new_player.password; return out; } QDataStream &operator>>(QDataStream &in, Player &existing_player) { in >> existing_player.name >> existing_player.password; return in; }
Here is the file writing code
QFile file(QDir::currentPath()+"Player.txt"); QDataStream out(&file); QDataStream out_dup(&file); if(file.open(QIODevice::ReadWrite)) { if(!exist(new_player,out_dup)) out << new_player; }
-
Hi
The body of the
QDataStream &operator<<(QDataStream &out, const Player &new_player)
{ ...goes into the .cpp file. Not the .h as then it will be defined multiple times.
so simply moved the functions into the cpp
-
@UG-SEP
Super.
Just for a heads up.
If you plan to change the data file format over time. its often a good idea to add a version number
https://doc.qt.io/qt-5/qdatastream.html#versioningThat was you can know if you app is given an older file as else it will just try to read the file in and odd stuff will happen if you change the order of members or added new one.