Results 1 to 10 of 10

Thread: QStandardItemModel save/load Problem

  1. #1
    Join Date
    Sep 2008
    Posts
    17
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question 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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  3. #3
    Join Date
    Sep 2008
    Posts
    17
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Angry Re: QStandardItemModel save/load Problem

    I gave it a try!

    My out Operator looks basically:

    Qt Code:
    1. for(int a=0;a<rowCount;a++)
    2. {
    3. //out << *myModel.item(a,0);
    4. out << *myModel.getItem(a);
    5. }
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QStandardItemModel save/load Problem

    Quote Originally Posted by sun View Post
    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.

  5. #5
    Join Date
    Sep 2008
    Posts
    17
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default 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!

  6. #6
    Join Date
    Sep 2008
    Posts
    17
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStandardItemModel save/load Problem

    still no luck!

  7. #7
    Join Date
    Sep 2008
    Posts
    17
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStandardItemModel save/load Problem

    Qt Code:
    1. QDataStream &operator<<(QDataStream &out, const Project &project)
    2. {
    3. QStandardItem temp = static_cast<QStandardItem >project;
    4. out << temp << project.getAttribut();
    5. return out;
    6. }
    To copy to clipboard, switch view to plain text mode 

    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?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QStandardItemModel save/load Problem

    Use pointers.

    Qt Code:
    1. QDataStream &operator<<(QDataStream &out, const Project &project) {
    2. QStandardItem *temp = static_cast<QStandardItem*>(&project);
    3. out << *temp << project.getAttribut();
    4. return out;
    5. }
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to wysota for this useful post:

    sun (1st October 2008)

  10. #9
    Join Date
    Sep 2008
    Posts
    17
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default 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?

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QStandardItemModel save/load Problem

    Quote Originally Posted by sun View Post
    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

Similar Threads

  1. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  2. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  3. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.