PDA

View Full Version : How to save QGraphicsScene Items to a file?



yagabey
25th February 2010, 22:31
Hello, I want to save all items in a QGraphicsScene to a file. When I load the file, I should be able to use them as QGraphicsItems(as before). I keep my items in a QList like QList<QGraphicsItem *> mItemsOnScreen. I should be able to get back that list when i load the file.

How can I save those items to a file on disk. What kind of a file format should i use? And of course how will i read that file back?

Thanks in advance...

JohannesMunk
26th February 2010, 00:08
To my knowledge there is no generic streaming for QGraphicsItems. How should there be?

You will have to write your own read and write methods, iterating through all items of the scene. When writing you save all properties of your items. And when reading from file you create your items one by one.

There are two major questions here:

1) How many different types of items do you have?
a) many: You should create a common abstract base class (multiple inheritance), which defines a way to write the specific item's properties to your file.
b) few: For the sake of simplicity you can write one global method in your scene, that identifies/casts your different types and handles them accordingly.

In both cases you will need a way to identify the types. You could use the type() method, which you need to implement anyway if you want to use qgraphicsitem_cast:


class A ..
{
public:
// Enables use of qgraphicsitem_cast()
enum { Type = UserType+1 };
int type() const { return Type;}
};

class B ..
{
public:
// Enables use of qgraphicsitem_cast()
enum { Type = UserType+2 };
int type() const { return Type;}
};

When reading you will have to read the item type separately, create an instance of the apropriate class and call its read function (1a) or set its properties directly (1b)..

2) How many items do you have in total?
a) lots >10k: I would opt for a binary file format. Look into QFile
b) few: Textfile. Maybe you could even use QSettings, which would give you an INI-File

Just some directions..

Johannes

lni
26th February 2010, 15:03
You can get hints on how qt designer save/load the ui file...

Slewman
26th February 2010, 15:10
i have a snippet of code i use to save AN ENTIRE scene, not sure if this will help, but it might
drawArea.front() is my GraphicsScene pointer


bool DrawArea::saveImage(const QString &fileName, const char *fileFormat)
{
QImage newImage(size(),QImage::Format_RGB32);
QPainter painter(&newImage);

drawArea.front()->render(&painter,drawArea.front()->sceneRect());
if (newImage.save(fileName,fileFormat,80))
{
qDebug()<<"SAVE SUCCESS!";
return true;
}
else
return false;
}

JohannesMunk
26th February 2010, 15:56
@Slewman: The question was to load/save the items as items.. not as image. You can't reconstruct his item list from an image!

@Ini: The idea is great, but have you actually tried to find the relevant piece of code? It's enormous! Look into qdesigner_resource.h/cpp and abstractformbuilder.h/cpp. However.. Qt opted for 1b. To centralize the specifics.

Cheers!

Johannes

yagabey
28th February 2010, 11:16
Thank you all. I think i will record all the items' properties imto an xml file. I had worried if there was an easier way.