PDA

View Full Version : Saving / Loading Files



nightroad
25th August 2011, 09:15
Hello there,

I need a suggestion about file saving-loading, first of all let me explain my case.

I have a QGraphicsView that showing many items in the Scenes. I need to save these QGraphicsItems into file and then i need to load back into Scenes then show these scenes into QgraphicsView.

I've already done saving data into file which is contains serialized QGraphicsItem as a QDataStream and then de-serialized these data into QGraphicsItems and showing into scenes. I'm keeping these data as like xml format let me show piece of my file format.



<!DOCTYPE NIGHTS>
<view>
<scene index="0">
<shape type="65537">AAAAJ0COMAAAAAAAQHtAAAAAAABAjjAAAAAAAEB7MAAAAAAAQI 44AAAAAABAexAAAAAAAECOQAAAAAAAQHsQAAAAAABAjkgAAAAA AEB7AAAAAAAAQI5QAAAAAABAewAAAAAAAECOYAAAAAAAQHrgAA AAAABAjnAAAAAAAEB6sAAAAAAAQI6gAAAAAABAekAAAAAAAECO 2AAAAAAAQHnQAAAAAABAjwAAAAAAAEB5UAAAAAAAQI9AAAAAAA BAeOAAAAAAAECPaAAAAAAAQHhQAAAAAABAj6gAAAAAAEB3sAAA AAAAQI/YAAAAAABAdvAAAAAAAECQBAAAAAAAQHZwAAAAAABAkBAAAAAAA EB14AAAAAAAQJAgAAAAAABAdXAAAAAAAECQMAAAAAAAQHUAAAA AAABAkCgAAAAAAEB1oAAAAAAAQJAoAAAAAABAdnAAAAAAAECQK AAAAAAAQHeAAAAAAABAkCgAAAAAAEB4sAAAAAAAQJAoAAAAAAB AehAAAAAAAECQNAAAAAAAQHtwAAAAAABAkDQAAAAAAEB80AAAA AAAQJA0AAAAAABAfoAAAAAAAECQNAAAAAAAQH+wAAAAAABAkDQ AAAAAAECAiAAAAAAAQJA0AAAAAABAgSAAAAAAAECQNAAAAAAAQ IGgAAAAAABAkDQAAAAAAECCOAAAAAAAQJA0AAAAAABAgsAAAAA AAECQNAAAAAAAQIMYAAAAAABAkDQAAAAAAECDcAAAAAAAQJA8A AAAAABAg8gAAAAAAECQPAAAAAAAQIQwAAAAAABAkDwAAAAAAEC EiAAAA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UAAAAAABAe9AAAA AAAECQoAAAAAAAQH0wAAAAAABAkLwAAAAAAEB+cAAAAAAAQJDo AAAAAABAf7AAAAAAAECRPAAAAAAAQICAAAAAAABAkZgAAAAAAE CBGAAAAAAAQJH8AAAAAABAgbAAAAAAAECSVAAAAAAAQIIgAAAA AABAkrAAAAAAAECCkAAAAAAAQJL4AAAAAABAgugAAAAAAECTFA AAAAAAQIMgAAAAAABAkygAAAAAAECDSAAAAAAAQJM4AAAAAABA g4AAAAAAAECTLAAAAAAAQIO4AAAAAABAkwgAAAAAAECDyAAAAA AAQJLcAAAAAABAg9gAAAAAAECSmAAAAAAAQIPoAAAAAABAklgA AAAAAECD+AAAAAAAQJIIAAAAAABAhBgAAAAAAECRvAAAAAAAQI QYAAAAAABAkYwAAAAAAECEKAAAAAAAQJFUAAAAAABAhEgAAAAA AECRJAAAAAAAQIRoAAAAAABAkPAAAAAAAECEkAAAAAAAP/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAChAEAiAAAAAAAAAQH//wAAAAAAAAAAP/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/wAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAA/8AAAAAAAAAA/8AAAAAAAAA==</shape>
</scene>
</view>


So there is element text which is base64 Binary data so i'm easily convertin that data into QDataStream then de-serializing QgraphicsItems.

Now, questions coming :) , Firstly That approac does make sense to you ? IF it didn't please could you give me better suggestion i can follow.
Second question when i save the QgraphicsItems into file or Loading back in that steps; as i said before, there is a lot of QGI almost 10k around. My gui is blocking event loop as you guess. But i'm not so experienced with QThreads could you give me clue, how can i pass data from Worker thread to Gui thread, in my situation.

Thanks, any suggestion is welcomed.

wysota
25th August 2011, 09:22
What you are doing now is ok but it requires quite lot of processing power to deserialize all the elements because you need to read them from the file (and they occupy twice as many bytes as they are worth since you are using base64), then you need to convert them to binary data and then create items. At least the first two steps can be moved to a worker thread feeding the main thread with already deserialized data so that it creates item out of the data. Then you wouldn't block the event loop and at the same time you would make use of two cores on a multicore machine.

yeye_olive
25th August 2011, 09:45
To answer your first question, I think you are abusing XML by filling it with base64-encoded gobbledygook. Either use XML with a human-readable format or switch to a binary format altogether.

About your second question. Have a look at http://www.qtcentre.org/wiki/index.php?title=Keeping_the_GUI_Responsive to review your options. If you end up choosing the worker thread route, you will face Qt's not-so-up-to-date documentation on the subject. Many threads on this forum deal with the problem of communication between threads so you could start from here.

nightroad
26th August 2011, 14:13
Hello there,
thanks for advice, I changed my approach so i'm keeping shapes into a binary files like that QVector< QVector< Shape *> > i'm serializing as like that. ContentManager is a QThread class.


bool ContentManager::load(const QString &fileName)
{
s_mutex.lock();
m_shapesFile->setFileName("c:/shapes.txt");
if (!m_shapesFile->open( QIODevice::ReadOnly )) {
return false;
}

QDataStream dsFile( m_shapesFile );
int boardCount;
int shapeCount;
int type;
dsFile >> sceneCount;
for (int i = 0; i < sceneCount; ++i)
{
dsFile >> shapeCount;
for (int j = 0; j < shapeCount; ++j)
{
dsFile >> type;
Shape *_s = new StrokeItem(); // TEST
dsFile >> _s; // TEST
delete _s; // TEST
}
}
qDebug() << "done";
m_shapesFile->close()
s_mutex.unlock();
return true;
}

Thus, I can read whole file correct as like that. So this is the question. What i must to do in TEST step. I can send binary data through signal to GUI thread and then de-serialize that QDataStream and then add that item into Scene or i can create new Shape *_s = new ... then Sending through that item into GUI thread then directl i can add this item into scene , What do you suggest for that, or do you have another an idea ?