PDA

View Full Version : I want to save and retrive a QGraphicsScene



c_srikanth1984
27th December 2008, 06:23
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

QFile file("fileName.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
if(scene->items().isEmpty()) //scene is QGraphicsScene variable
return 0;
else
QList<QGraphicsItem *>itemsList=scene->items();
out<<itemsList;

My code for opening the saved file is

QString fileName=QFileDialog::getOpenFileName(this,tr("Open File"),QDir::currentPath()):
QFile file(fileName);
if(file.open(QIODevice::ReadOnly))
{
QMessageBox::information(this,tr("Unable to open");
return 0;
}
else
{
QList<QGraphicsItem *>itemsList=scene->items();
QDataStream in(&file);
in>>itemsList;
foreach(QGraphicsItem *item,itemList)
scene->addItem(item);
file.close();
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

jpn
27th December 2008, 07:10
Consider serializing the necessary _data_ instead of pointers.

c_srikanth1984
27th December 2008, 17:13
hi Jpn thanx for replying,
Could u plz be clear coz i am new to Qt.
thank u
with Regards,
srikanth

aamer4yu
29th December 2008, 07:41
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_

c_srikanth1984
29th December 2008, 10:35
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

aamer4yu
29th December 2008, 13:20
Is ur if condition proper ?

if(file.open(QIODevice::ReadOnly))
shoudnt it be
if(!file.open(QIODevice::ReadOnly)) ??

c_srikanth1984
30th December 2008, 04:19
Ya its ok i have missed it. my problem is with QDataStream object.

in>>itemsList;
i am not able to read my saved list from the datastream.
can someone help me.

jpn
30th December 2008, 07:05
my problem is with QDataStream object.

in>>itemsList;
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.

c_srikanth1984
30th December 2008, 07:26
k Thank u JPN i will try that way.

c_srikanth1984
30th December 2008, 08:33
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.

jpn
30th December 2008, 08:59
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?

c_srikanth1984
30th December 2008, 10:31
k I agree with u. but how to solve my problem. I am getting a pointer based list item when I am calling
QList<QGraphicsItem *>itemsList=scene->items(); i am not getting an idea to achive my task.

jpn
30th December 2008, 10:42
You can store things like:

QGraphicsItem::type()
QGraphicsItem::pos()
...

for each item in the list. Store whatever is enough to recreate your items.

robertson1
30th December 2008, 10:58
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.

c_srikanth1984
31st December 2008, 04:11
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 .

c_srikanth1984
31st December 2008, 04:13
Hi Robertson,
thank u for replying.
I got it now.
with regards,
srikanth

vinzzz
7th January 2014, 14:19
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