Results 1 to 4 of 4

Thread: Saving / Loading Files

  1. #1
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Saving / Loading Files

    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.

    Qt Code:
    1. <!DOCTYPE NIGHTS>
    2. <view>
    3. <scene index="0">
    4. <shape type="65537">AAAAJ0COMAAAAAAAQHtAAAAAAABAjjAAAAAAAEB7MAAAAAAAQI44AAAAAABAexAAAAAAAECOQAAAAAAAQHsQAAAAAABAjkgAAAAAAEB7AAAAAAAAQI5QAAAAAABAewAAAAAAAECOYAAAAAAAQHrgAAAAAABAjnAAAAAAAEB6sAAAAAAAQI6gAAAAAABAekAAAAAAAECO2AAAAAAAQHnQAAAAAABAjwAAAAAAAEB5UAAAAAAAQI9AAAAAAABAeOAAAAAAAECPaAAAAAAAQHhQAAAAAABAj6gAAAAAAEB3sAAAAAAAQI/YAAAAAABAdvAAAAAAAECQBAAAAAAAQHZwAAAAAABAkBAAAAAAAEB14AAAAAAAQJAgAAAAAABAdXAAAAAAAECQMAAAAAAAQHUAAAAAAABAkCgAAAAAAEB1oAAAAAAAQJAoAAAAAABAdnAAAAAAAECQKAAAAAAAQHeAAAAAAABAkCgAAAAAAEB4sAAAAAAAQJAoAAAAAABAehAAAAAAAECQNAAAAAAAQHtwAAAAAABAkDQAAAAAAEB80AAAAAAAQJA0AAAAAABAfoAAAAAAAECQNAAAAAAAQH+wAAAAAABAkDQAAAAAAECAiAAAAAAAQJA0AAAAAABAgSAAAAAAAECQNAAAAAAAQIGgAAAAAABAkDQAAAAAAECCOAAAAAAAQJA0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QJEsAAAAAABAdsAAAAAAAECREAAAAAAAQHdAAAAAAABAkPAAAAAAAEB4AAAAAAAAQJDQAAAAAABAeKAAAAAAAECQrAAAAAAAQHmQAAAAAABAkJwAAAAAAEB6oAAAAAAAQJCUAAAAAABAe9AAAAAAAECQoAAAAAAAQH0wAAAAAABAkLwAAAAAAEB+cAAAAAAAQJDoAAAAAABAf7AAAAAAAECRPAAAAAAAQICAAAAAAABAkZgAAAAAAECBGAAAAAAAQJH8AAAAAABAgbAAAAAAAECSVAAAAAAAQIIgAAAAAABAkrAAAAAAAECCkAAAAAAAQJL4AAAAAABAgugAAAAAAECTFAAAAAAAQIMgAAAAAABAkygAAAAAAECDSAAAAAAAQJM4AAAAAABAg4AAAAAAAECTLAAAAAAAQIO4AAAAAABAkwgAAAAAAECDyAAAAAAAQJLcAAAAAABAg9gAAAAAAECSmAAAAAAAQIPoAAAAAABAklgAAAAAAECD+AAAAAAAQJIIAAAAAABAhBgAAAAAAECRvAAAAAAAQIQYAAAAAABAkYwAAAAAAECEKAAAAAAAQJFUAAAAAABAhEgAAAAAAECRJAAAAAAAQIRoAAAAAABAkPAAAAAAAECEkAAAAAAAP/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAChAEAiAAAAAAAAAQH//wAAAAAAAAAAP/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/wAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAA/8AAAAAAAAAA/8AAAAAAAAA==</shape>
    11. </scene>
    12. </view>
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    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: Saving / Loading Files

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Saving / Loading Files

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

  4. #4
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Saving / Loading Files

    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.

    Qt Code:
    1. bool ContentManager::load(const QString &fileName)
    2. {
    3. s_mutex.lock();
    4. m_shapesFile->setFileName("c:/shapes.txt");
    5. if (!m_shapesFile->open( QIODevice::ReadOnly )) {
    6. return false;
    7. }
    8.  
    9. QDataStream dsFile( m_shapesFile );
    10. int boardCount;
    11. int shapeCount;
    12. int type;
    13. dsFile >> sceneCount;
    14. for (int i = 0; i < sceneCount; ++i)
    15. {
    16. dsFile >> shapeCount;
    17. for (int j = 0; j < shapeCount; ++j)
    18. {
    19. dsFile >> type;
    20. Shape *_s = new StrokeItem(); // TEST
    21. dsFile >> _s; // TEST
    22. delete _s; // TEST
    23. }
    24. }
    25. qDebug() << "done";
    26. m_shapesFile->close()
    27. s_mutex.unlock();
    28. return true;
    29. }
    To copy to clipboard, switch view to plain text mode 

    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 ?
    Last edited by nightroad; 26th August 2011 at 13:18.

Similar Threads

  1. Prevent saving files to System folders
    By ScottBrady in forum Newbie
    Replies: 8
    Last Post: 13th August 2011, 15:19
  2. Problem when saving bmp files
    By cdlaweed in forum Newbie
    Replies: 3
    Last Post: 17th March 2011, 09:20
  3. Replies: 20
    Last Post: 22nd February 2011, 17:01
  4. Loading and saving in-memory SQLITE databases
    By miraks in forum Qt Programming
    Replies: 10
    Last Post: 27th April 2010, 21:24
  5. QDataStream and saving and loading QList
    By Noxxik in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2009, 22:02

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.