Results 1 to 15 of 15

Thread: QGraphicsView Copy and Paste

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: QGraphicsView Copy and Paste

    Think about what you are doing in the serialization code you posted:

    Qt Code:
    1. QFile file("fileName.dat");
    2. file.open(QIODevice::WriteOnly);
    3. QDataStream out(&file);
    4.  
    5. QList<QGraphicsItem *> list1= this->selectedItems();
    6. itemListSize = list1.size();
    7. out<<itemListSize;
    8. foreach( QGraphicsItem* item, list1)
    9. {
    10. out << item->x();
    11. out << item->y();
    12. }
    To copy to clipboard, switch view to plain text mode 

    What's going to be in this file after you are finished? In case you can't guess, it starts with an integer (the number of selected graphics items), and then continues with a series of floating point numbers, two for every item in the list. That's it. Absolutely no information about what kind of graphics item you are serializing (or even that the file results from serializing anything). So how can you possibly expect that the code that reads it in will have any clue what to do with it?

    If what you want to do is save the state of the selected items to a file, and then sometime later, read that file and restore those items to exactly the same state, then, at a minimum, you need to save:

    1 - the number of items (you did that part OK)
    2 - for each item:

    - what kind of item it is
    - every property needed to re-create that item with the same appearance and put it in the right place in the scene (like, including its place in the object hierarchy)


    And when you restore the items from the file, you need to reverse this process (and by the way, your de-serialization code is buggy: what happens if the currently selected items list is a different size from what is in the file, or the selected item types don't match?)

    1 - read the number of items to restore
    2 - for each item:

    - read the item's type
    - create an item of that type
    - read the properties
    - set the properties (correctly) on the item you just created
    - add the item to the scene (in the correct place in the object hierarchy)

  2. #2
    Join Date
    Jan 2014
    Posts
    6
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows

    Default Re: QGraphicsView Copy and Paste

    thanks d_stranz now i am getting what i have done and what i am supposed to do but still i have confusions


    This is what i did for serializing the items

    Qt Code:
    1. QFile file("fileName.dat");
    2. file.open(QIODevice::WriteOnly);
    3. QDataStream out(&file);
    4.  
    5. QList<QGraphicsItem *> list1= this->selectedItems();
    6.  
    7. itemListSize = list1.size();
    8. DiagramItem *itm = static_cast<DiagramItem *>(list1.front());
    9. out<<itemListSize;
    10. foreach( QGraphicsItem* item, list1)
    11. {
    12. QPointF sp = itm->scenePos();
    13.  
    14.  
    15. myItemType1=itm->myDiagramType;
    16.  
    17. out << sp ;
    18. out << myItemType;
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

    and for de-serialization i implemented this code

    Qt Code:
    1. QFile file("fileName.dat");
    2. QList<QGraphicsItem *> list1 = this->selectedItems();
    3. file.open(QIODevice::ReadOnly);
    4. QDataStream in(&file);
    5. in>>itemListSize;
    6.  
    7. foreach(QGraphicsItem *item, list1)
    8. {
    9. in >> sp;
    10.  
    11.  
    12. // in>> myItemType; // i dono whats the problem in de-serialization of this "myItemType"
    13.  
    14. item1 = new DiagramItem(myItemType1);
    15. addItem(item1);
    16. item1->setPos(sp);
    17. item1->moveBy(50 ,50);
    18.  
    19.  
    20. }
    21.  
    22. file.close();
    To copy to clipboard, switch view to plain text mode 

    this code does not performing proper serialization, i am able to copy the item but couldn't paste it... what i have done is

    In the 1st part i am casting the list into DiagramItem and serializing those properties of that class, in the 2nd part i am not casting back the DiagramItem back to list is that the problem i am facing....?
    Is cut functionality can be performed, if yes just give some directions for it....?


    thanx in advance
    Last edited by vinzzz; 13th January 2014 at 10:43.

Similar Threads

  1. using cut(), copy(), paste()
    By systemz89 in forum Newbie
    Replies: 5
    Last Post: 18th December 2007, 15:47

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.