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 2014
    Posts
    6
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows

    Default Re: QGraphicsView Copy and Paste

    hi i am a new to Qt, i couldn’t find how u will serialize selected items, if you copy an selected item like which i done below i just get a shallow copy of an item, how do we actually get this selected item and serialize it. plz show it with a small example

    QList<QGraphicsItem *> copiedItem = this->selectedItems();

    All i want to finally do is, i wanted to copy and paste QGraphicItem on my QGraphicScene .
    thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView Copy and Paste

    You have to serialize the item using QDataStream in your own code. Serializing a list of pointers is exactly that -- serializing a list of pointers, not a list of objects. There is no ready code to serialize any possible graphics item, you need to write code dedicated to your item classes.
    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
    Jan 2014
    Posts
    6
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows

    Default Re: QGraphicsView Copy and Paste

    This is how i copy an item and serialize it

    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 

    and for pasting it back i use the following 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. foreach(QGraphicsItem *item, list1)
    7. {
    8. in >> item->x();
    9. in >> item->y();
    10.  
    11. }
    12.  
    13. scene1->addItem(item);
    14. file.close();
    To copy to clipboard, switch view to plain text mode 


    but i cannot get the items x and y position while reading data. Am i serializing in the correct way, plz help me out.
    Last edited by vinzzz; 8th January 2014 at 11:46.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView Copy and Paste

    Your deserializing code is incorrect. To set x and y values you need to use setX() and setY() and not redirect to x() and y(). Moreover this code won't work because you're adding the item outside the for loop.
    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.


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

    Default Re: QGraphicsView Copy and Paste

    sorry wysota i am new to Qt plz elaborate it, and show me any small example for serialization and deserialization

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView Copy and Paste

    Quote Originally Posted by vinzzz View Post
    sorry wysota i am new to Qt
    Your problem is C++ not Qt.

    and show me any small example for serialization and deserialization
    There are examples in QDataStream docs.
    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.


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

    Default Re: QGraphicsView Copy and Paste

    Ya now that error has been cleared, but i could'nt get what attributes should i serialize. below is the code how i create a rect item

    This is the function written in my mousePressEvent


    Qt Code:
    1. {
    2. if (mouseEvent->button() != Qt::LeftButton)
    3. return;
    4.  
    5. DiagramItem *item;
    6. switch (myMode) {
    7. case InsertItem:
    8. item = new DiagramItem(myItemType);
    9. item->setBrush(myItemColor);
    10. addItem(item);
    11. item->setPos(mouseEvent->scenePos());
    12. emit itemInserted(item);
    13. break;
    14.  
    15. ;
    16. }
    17.  
    18. QGraphicsScene::mousePressEvent(mouseEvent);
    19. }
    To copy to clipboard, switch view to plain text mode 

    This is the code written for creating a polygon item in my DiagramItem's class constructor

    Qt Code:
    1. myDiagramType = diagramType;
    2. switch (myDiagramType) {
    3. case Step:
    4. myPolygon << QPointF(-100, -100) << QPointF(100, -100)
    5. << QPointF(100, 100) << QPointF(-100, 100)
    6. << QPointF(-100, -100);
    7. }
    8. setPolygon(myPolygon);
    9.  
    10. setFlag(QGraphicsItem::ItemIsMovable, true);
    11. setFlag(QGraphicsItem::ItemIsSelectable, true);
    12. setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    I am getting a perfect rect but could'nt cut, copy and paste my QRect Item

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,324
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    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)

  9. #9
    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 09:43.

Similar Threads

  1. using cut(), copy(), paste()
    By systemz89 in forum Newbie
    Replies: 5
    Last Post: 18th December 2007, 14: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.