QTcpSocket* TCPSocket = (QTcpSocket*)sender(); QDataStream in(TCPSocket);
in>>data.b_dir;in>>data.b_posx;in>>data.b_posy;in>>data.player;in>>data.t_dir;in>>data.t_movex;in>>data.t_movey;in>>data.t_posx;in>>data.t_posy;in>>data.startflag;in>>data.t_dead;
QVector < Input *> data;
struct Input{ bool startflag; int player; bool t_dead; int t_dir; int t_posx; int t_posy; int t_movex; int t_movey; int b_posx; int b_posy; int b_dir;};
struct promp{public: int val1; int val2; bool val3;};
QDataStream &operator>>(QDataStream &in, promp &inp){ in>>inp.val1; in>>inp.val2; in>>inp.val3; return in;}QDataStream &operator>>(QDataStream &in, promp *inp){ in>>inp->val1; in>>inp->val2; in>>inp->val3; return in;}QDataStream &operator<<(QDataStream &out, const promp &inp){ out<<inp.val1<<inp.val2; out<<inp.val3; return out;}QDataStream &operator<<(QDataStream &out, const promp *inp){ out<<inp->val1<<inp->val2; out<<inp->val3; return out;}int main(int argc,char** argv){ QApplication app(argc,argv); promp p1; promp *p2= new promp(); p1.val1=1;p2->val1=2; p1.val2=10;p2->val2=20; p1.val2=true;p2->val3=false; QFile f("temp.t"); { f.open(QIODevice::WriteOnly); f.resize(0); QDataStream s(&f); s<<p1; f.close(); } { f.open(QIODevice::ReadOnly); QDataStream s(&f); s>>p2; f.close(); qWarning()<<"p1 write, p2 read\t"<< p2->val1 << p2->val2 << p2->val3; } { p2->val1=2;p2->val2=20;p2->val3=false; f.open(QIODevice::WriteOnly); f.resize(0); QDataStream s(&f); s<<p2; f.close(); } { f.open(QIODevice::ReadOnly); QDataStream s(&f); s>>p1; f.close(); qWarning()<<"p2 write, p1 read\t"<< p1.val1 << p1.val2 << p1.val3; } return 1;}
Запускается D:\qt\projects\LIC_NEW\release\CRYPTER.exe...p1 write, p2 read 1 1 true p2 write, p1 read 2 20 false D:\qt\projects\LIC_NEW\release\CRYPTER.exe завершился с кодом 1
int main(int argc,char** argv){ QApplication app(argc,argv); promp p1,p2,p3,p4,p5; p1.val1=1; p1.val2=10; p1.val3=true; p2.val1=2; p2.val2=20; p2.val3=true; p3.val1=3; p3.val2=30; p3.val3=true; p4.val1=4; p4.val2=40; p4.val3=true; p5.val1=5; p5.val2=50; p5.val3=true; QVector<promp> players; players.append(p1); players.append(p2); players.append(p3); players.append(p4); players.append(p5); QFile f("temp.t"); { f.open(QIODevice::WriteOnly); f.resize(0); QDataStream s(&f); s<<players; f.close(); } p1.val1=0; p1.val2=0; p1.val3=false; p2.val1=0; p2.val2=0; p2.val3=false; p3.val1=0; p3.val2=0; p3.val3=false; p4.val1=0; p4.val2=0; p4.val3=false; p5.val1=0; p5.val2=0; p5.val3=false; players.clear(); { f.open(QIODevice::ReadOnly); QDataStream s(&f); s>>players; f.close(); for(int i=0; i<players.size();i++) qWarning()<<"p"<<i<<"\t"<< players.at(i).val1 << players.at(i).val2 << players.at(i).val3; } return 1;}
Запускается D:\qt\projects\LIC_NEW\release\CRYPTER.exe...p 0 1 10 true p 1 2 20 true p 2 3 30 true p 3 4 40 true p 4 5 50 true D:\qt\projects\LIC_NEW\release\CRYPTER.exe завершился с кодом 1
struct MyStruct { int i; ... }; Q_DECLARE_METATYPE(MyStruct)