Results 1 to 17 of 17

Thread: I want to save and retrive a QGraphicsScene

  1. #1
    Join Date
    Dec 2008
    Posts
    52
    Thanks
    3
    Qt products
    Qt4

    Default I want to save and retrive a QGraphicsScene

    Hi Friends,
    I am working to creat an application where I need to have a scene where i am going to place QGraphicsItems on that scene dynamically then I need to save that scene and retrive that scene as a QGraphicsScene only wher user can modify the graphics Item again and save.
    now my code for saving is
    Qt Code:
    1. QFile file("fileName.dat");
    2. file.open(QIODevice::WriteOnly);
    3. QDataStream out(&file);
    4. if(scene->items().isEmpty()) //scene is QGraphicsScene variable
    5. return 0;
    6. else
    7. QList<QGraphicsItem *>itemsList=scene->items();
    8. out<<itemsList;
    To copy to clipboard, switch view to plain text mode 
    My code for opening the saved file is
    Qt Code:
    1. QString fileName=QFileDialog::getOpenFileName(this,tr("Open File"),QDir::currentPath()):
    2. QFile file(fileName);
    3. if(file.open(QIODevice::ReadOnly))
    4. {
    5. QMessageBox::information(this,tr("Unable to open");
    6. return 0;
    7. }
    8. else
    9. {
    10. QList<QGraphicsItem *>itemsList=scene->items();
    11. QDataStream in(&file);
    12. in>>itemsList;
    13. foreach(QGraphicsItem *item,itemList)
    14. scene->addItem(item);
    15. file.close();
    To copy to clipboard, switch view to plain text mode 
    Thats all logically I think I am Correct but I am Getting an error
    "no match for 'operator>>' in 's>>t' in qdatastream.h

    Plz help me friends where i am going wrong or is there any other method for my requirement.
    with Regards,
    srikanth
    Last edited by jpn; 27th December 2008 at 06:25. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: I want to save and retrive a QGraphicsScene

    Consider serializing the necessary _data_ instead of pointers.
    J-P Nurmi

  3. #3
    Join Date
    Dec 2008
    Posts
    52
    Thanks
    3
    Qt products
    Qt4

    Default Re: I want to save and retrive a QGraphicsScene

    hi Jpn thanx for replying,
    Could u plz be clear coz i am new to Qt.
    thank u
    with Regards,
    srikanth

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I want to save and retrive a QGraphicsScene

    Ur item must contain data about itself, like position, shape, color, etc.

    You need to save these _data_ in the file adn when reading from it, construct the items again from the _data_

  5. #5
    Join Date
    Dec 2008
    Posts
    52
    Thanks
    3
    Qt products
    Qt4

    Default Re: I want to save and retrive a QGraphicsScene

    Hi Aamer,
    Thanx for replying,
    but i am facing problem when i am reading the graphicsItem list from datastream object(in>>itemList). Wat u said is the problem for that error.
    with regards,
    srikanth

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I want to save and retrive a QGraphicsScene

    Is ur if condition proper ?
    if(file.open(QIODevice::ReadOnly))
    shoudnt it be
    if(!file.open(QIODevice::ReadOnly))
    ??

  7. #7
    Join Date
    Dec 2008
    Posts
    52
    Thanks
    3
    Qt products
    Qt4

    Default Re: I want to save and retrive a QGraphicsScene

    Ya its ok i have missed it. my problem is with QDataStream object.
    Qt Code:
    1. in>>itemsList;
    To copy to clipboard, switch view to plain text mode 
    i am not able to read my saved list from the datastream.
    can someone help me.

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: I want to save and retrive a QGraphicsScene

    Quote Originally Posted by c_srikanth1984 View Post
    my problem is with QDataStream object.
    Qt Code:
    1. in>>itemsList;
    To copy to clipboard, switch view to plain text mode 
    i am not able to read my saved list from the datastream.
    can someone help me.
    Believe me, it won't work that way. You cannot save pointers to a file and reload it after restarting the application. You have to save necessary attributes so that you can re-create the items when loading a file.
    J-P Nurmi

  9. #9
    Join Date
    Dec 2008
    Posts
    52
    Thanks
    3
    Qt products
    Qt4

    Default Re: I want to save and retrive a QGraphicsScene

    k Thank u JPN i will try that way.

  10. #10
    Join Date
    Dec 2008
    Posts
    52
    Thanks
    3
    Qt products
    Qt4

    Default Re: I want to save and retrive a QGraphicsScene

    HI JPN,
    i have created my application where Graphics view,scene and Items are created using pointers. I have no idea how to create these with out using pointers. I am using Qt4.4.3.
    JPN plz do me a favour na plz u creat small application with a Graphics view, place atleast 2 graphics item on to it and save it through datastream obj. and retrive it.
    not only JPN any one know how to do can help me.
    with regards,
    srikanth.

  11. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: I want to save and retrive a QGraphicsScene

    I'm not saying that you should allocate graphics items on the stack, not at all. I'm just saying that saving pointers to a file doesn't make sense. Just think of it, what is a pointer?
    J-P Nurmi

  12. #12
    Join Date
    Dec 2008
    Posts
    52
    Thanks
    3
    Qt products
    Qt4

    Default Re: I want to save and retrive a QGraphicsScene

    k I agree with u. but how to solve my problem. I am getting a pointer based list item when I am calling
    Qt Code:
    1. QList<QGraphicsItem *>itemsList=scene->items();
    To copy to clipboard, switch view to plain text mode 
    i am not getting an idea to achive my task.

  13. #13
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: I want to save and retrive a QGraphicsScene

    You can store things like:

    for each item in the list. Store whatever is enough to recreate your items.
    J-P Nurmi

  14. #14
    Join Date
    Nov 2007
    Posts
    26
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I want to save and retrive a QGraphicsScene

    As the posts above say, you cant serialize QGraphicsItems directly with QDataStream.

    see http://doc.trolltech.com/4.4/datastreamformat.html

    For example to save a QGraphicsRectItem, you could serialize a QRect along values like position rotation, scale etc. Read in these values and create a new QGraphicsRectItem with them.

  15. #15
    Join Date
    Dec 2008
    Posts
    52
    Thanks
    3
    Qt products
    Qt4

    Default Re: I want to save and retrive a QGraphicsScene

    Hi JPN,
    now i got an idea how to do. all these time i was thinking of saving an item but now i understood that we need to save its properties and re-construct the item again in new scene.
    thank u,
    with regards,
    srikanth .

  16. #16
    Join Date
    Dec 2008
    Posts
    52
    Thanks
    3
    Qt products
    Qt4

    Default Re: I want to save and retrive a QGraphicsScene

    Hi Robertson,
    thank u for replying.
    I got it now.
    with regards,
    srikanth

  17. #17
    Join Date
    Jan 2014
    Posts
    6
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows

    Default Re: I want to save and retrive a QGraphicsScene

    hey i am trying the same functionality but couldn't succeeded and the above link is also crashed .. plz help me out

    thanks in advance

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.