PDA

View Full Version : QStandardItemModel save/load Problem



sun
29th September 2008, 10:16
Hallo,

i subclassed from QStandardItemModel and want to save the contents of this Model to a File.
I guess I have to implement:

QDataStream &operator<<(QDataStream &out, const myModel &model);
QDataStream &operator>>(QDataStream &in, myModel &model);

Do I have to manually pick the (custom myStandardItem) items and put them in a File, or is there any simpler way to do this?

Thanks in advance
sun

wysota
29th September 2008, 10:25
Yes, you have to do that manually, but you can use QVariant's serializing methods to ease your life a bit.

sun
29th September 2008, 13:14
I gave it a try!

My out Operator looks basically:


for(int a=0;a<rowCount;a++)
{
//out << *myModel.item(a,0);
out << *myModel.getItem(a);
}

With the first out uncommented the File gets bigger. I think this executes the QStandardItems operator, but the specific myItem Attributs get lost :(

When I cast the QStandardItem to myItem in myModel.getItem it executes my myItem in operator, but all QStandardItem specific things get lost.

How to implement the myItem &operator<< and the myModel &operator<<, so that i save my Attributs and also save the QstandardItem (model) attributes?

sun

wysota
29th September 2008, 23:03
How to implement the myItem &operator<< and the myModel &operator<<, so that i save my Attributs and also save the QstandardItem (model) attributes?

Overload the << operator and call the QStandardItem implementation inside.

sun
30th September 2008, 07:27
I'm trying to do that but have no luck.
I'm not allowed to cast the myItem to QStandardItem (because is is const?) and dont know how to call the Operator otherwise.
Coming from Java I have some Problems here.

Will now search the Internet and report if I have any luck.
Any help on howto do this is still appreciated!

sun
30th September 2008, 11:54
still no luck! :(

sun
30th September 2008, 15:30
QDataStream &operator<<(QDataStream &out, const Project &project)
{
QStandardItem temp = static_cast<QStandardItem >project;
out << temp << project.getAttribut();
return out;
}

I tried it somehow like that. to Cast in any way the project which is a subclass of QStandardItem to a QStandardItem, because then the << should write out the object with the QStandardItem &operator<<.
But the casts I tried don't compile.

Seems to be a c++ issue how to get this working?

wysota
30th September 2008, 23:23
Use pointers.


QDataStream &operator<<(QDataStream &out, const Project &project) {
QStandardItem *temp = static_cast<QStandardItem*>(&project);
out << *temp << project.getAttribut();
return out;
}

sun
1st October 2008, 08:50
looks like i nearly got it :)
But I think I have to use const_cast.

With a const_cast to Project it seems to work, but I realised that I made a big mistake.
My Projectclass inherits QStandardItem AND QObject.
So I can't make an QStandardItem from a Project. Or am I wrong?

So I now face the descision, to implement a &operator<< with the relevant Attributes of the QStandarditem on my own or to get rid of the QObject inheritance what would make necessary massive changes to my information flux.
Is it bad Style to Make QStandardItems to QObjects?

wysota
1st October 2008, 18:20
looks like i nearly got it :)
But I think I have to use const_cast.
No you don't. Simply add "const" at the begginning of the second line of the snippet I posted.


My Projectclass inherits QStandardItem AND QObject.
So I can't make an QStandardItem from a Project. Or am I wrong?
You are wrong. You are wrong again to make your item inherit QObject. I really wouldn't advise it.


So I now face the descision, to implement a &operator<< with the relevant Attributes of the QStandarditem on my own or to get rid of the QObject inheritance what would make necessary massive changes to my information flux.
Get rid of QObject heritage.

Is it bad Style to Make QStandardItems to QObjects?

I'd say it's worse than Bad :)