Results 1 to 6 of 6

Thread: How to save QGraphicsScene Items to a file?

  1. #1
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default How to save QGraphicsScene Items to a file?

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

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to save QGraphicsScene Items to a file?

    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:

    Qt Code:
    1. class A ..
    2. {
    3. public:
    4. // Enables use of qgraphicsitem_cast()
    5. enum { Type = UserType+1 };
    6. int type() const { return Type;}
    7. };
    8.  
    9. class B ..
    10. {
    11. public:
    12. // Enables use of qgraphicsitem_cast()
    13. enum { Type = UserType+2 };
    14. int type() const { return Type;}
    15. };
    To copy to clipboard, switch view to plain text mode 

    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

  3. The following user says thank you to JohannesMunk for this useful post:

    congzhou09 (4th January 2012)

  4. #3
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to save QGraphicsScene Items to a file?

    You can get hints on how qt designer save/load the ui file...

  5. #4
    Join Date
    Sep 2009
    Posts
    64

    Default Re: How to save QGraphicsScene Items to a file?

    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
    Qt Code:
    1. bool DrawArea::saveImage(const QString &fileName, const char *fileFormat)
    2. {
    3. QImage newImage(size(),QImage::Format_RGB32);
    4. QPainter painter(&newImage);
    5.  
    6. drawArea.front()->render(&painter,drawArea.front()->sceneRect());
    7. if (newImage.save(fileName,fileFormat,80))
    8. {
    9. qDebug()<<"SAVE SUCCESS!";
    10. return true;
    11. }
    12. else
    13. return false;
    14. }
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to save QGraphicsScene Items to a file?

    @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

  7. #6
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: How to save QGraphicsScene Items to a file?

    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.

Similar Threads

  1. I want to save and retrive a QGraphicsScene
    By c_srikanth1984 in forum Qt Programming
    Replies: 16
    Last Post: 7th January 2014, 14:19
  2. Replies: 1
    Last Post: 29th January 2010, 13:44
  3. Rendering QGraphicsScene to QPixmap to save
    By Rooster in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2009, 05:24
  4. How could I save Items on a QGraphicsScene?
    By pinkfrog in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2009, 05:03
  5. Can I save the drawings from QGraphicsScene?
    By learning_qt in forum Qt Programming
    Replies: 1
    Last Post: 19th November 2008, 11:03

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
  •  
Qt is a trademark of The Qt Company.