QStandardItemModel save/load Problem
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
Re: QStandardItemModel save/load Problem
Yes, you have to do that manually, but you can use QVariant's serializing methods to ease your life a bit.
Re: QStandardItemModel save/load Problem
I gave it a try!
My out Operator looks basically:
Code:
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
Re: QStandardItemModel save/load Problem
Quote:
Originally Posted by
sun
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.
Re: QStandardItemModel save/load Problem
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!
Re: QStandardItemModel save/load Problem
Re: QStandardItemModel save/load Problem
Code:
{
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?
Re: QStandardItemModel save/load Problem
Use pointers.
Code:
out << *temp << project.getAttribut();
return out;
}
Re: QStandardItemModel save/load Problem
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?
Re: QStandardItemModel save/load Problem
Quote:
Originally Posted by
sun
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.
Quote:
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.
Quote:
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.
Quote:
Is it bad Style to Make QStandardItems to QObjects?
I'd say it's worse than Bad :)