PDA

View Full Version : Class serialization



matulik
10th December 2010, 16:36
Hello.
I have class like this:

class pytanie
{
public:
int ID;
QString pytanie;
QString odp1;
QString odp2;
QString odp3;
QString odp4;
};

and I creating a dynamic array by QVector like this:

QVector<pytanie> pyt;

Now, I want to save (and serialize) objects of this classes to file. I'm using QDataStream. I'm trying few time, buy I still can't do it.

I trying for example:

QDataStream out (&plik);
out << pyt[x];
but it doesnt work..

Anyone can help me?

Lykurg
10th December 2010, 16:38
you have to provide the "QDataStream << operator" for your class. Search the forum, about a year ago there where similar questions (with answers).

matulik
11th December 2010, 20:43
I know, I tried to do it, but I don't know how to do it exactly.
For example:


QDataStream out(&file);
out.operator <<(QVector<pytanie> & pyt[x]);


But this doesn't work... :(

wysota
11th December 2010, 22:30
You need to define an operator, not try using something which already exists (ignoring the fact that this code is not even syntactically correct).

matulik
12th December 2010, 22:59
OK. To understand this, I was trying to save QVector<int> object.
So I created two function to save and load:


void MainWindow::save()
{
QFile file("path....");
if(!plik.open(QIODevice::ReadWrite))
return;
else
{
QDataStream out(&file);
QDataStream & operator<<(QDataStream & out, const QVector<int> & asd);
out << asd[0];

qDebug()<<"ok";
plik.close();
}
}



void MainWindow::load()
{
QFile plik("path....");
if(!plik.open(QIODevice::ReadWrite))
return;
else
{
QDataStream in(&file);
QDataStream & operator>>(QDataStream & in, QVector<int> & asd);

asd.insert(0,1);
in >> asd [0];

qDebug()<<"ok!";
}
}

and this working great (now for only one item in array).
But now I have two question about that:
1. Can't I save all of asd objects (without loop of asd[x])?
2. What I can save object of QVector<myOwnClass>?

I tried:


QDataStream out(&plik);
QDataStream & operator<<(QDataStream & out, const QVector<pytania> & pyt);
out << pyt[0];


but this doesn't works..

wysota
13th December 2010, 00:42
QDataStream out(&file);
QDataStream & operator<<(QDataStream & out, const QVector<int> & asd);



QDataStream in(&file);
QDataStream & operator>>(QDataStream & in, QVector<int> & asd);

What is this supposed to do? Do you understand what you have written here? Also do you understand what we mean when we say you should implement QDataStream streaming operators for your class?

marcvanriet
13th December 2010, 12:17
Also do you understand what we mean when we say you should implement QDataStream streaming operators for your class?

Obviously not. This is more of a retorical question, no ?

@ matulik : you can't just create a class and expect << to work on it. the << operator is sort of like a normal method of your class (not really, but I'm just making a point). You must define this operator for your class, just like you define other methods of your class.

You need to find yourself a book or tutorial on C++ if you want to understand how to do this. This is a pure C++ question, unrelated to Qt.

Best regards,
Marc