Results 1 to 15 of 15

Thread: QGraphicsView Copy and Paste

  1. #1
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Smile QGraphicsView Copy and Paste

    I need to implement copying and pasting the contents of a QGraphicsView, and/or just the selected QGraphicsItems. If anyone has any suggestions or pointers as to how to do this, I would appreciate hearing about it. I think I am going to need to implement a custom mime type to put this stuff on the clipboard. My QGraphicsItems are all overridden non-standard items, and I guess each one should have it's own copy method.

    If someone could point me at some sample code, or offer some starting suggestions, I would appreciate it.

  2. #2
    Join Date
    Jan 2008
    Location
    China
    Posts
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QGraphicsView Copy and Paste

    you can save selected item's attribute in the QClipboardord when you save,and instantiate an object with the attribute in the QClipboardord when you paste;
    Or make a flag = 1 when copy,and instantiate an object with the attribute by selected item's pointer and make flag = 0 when paste .hope can help you

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView Copy and Paste

    Firstly, graphicsView only shows the content of a scene. Items actually are in a scene.
    Do u want copy-paste or drag drop ??

    For copy paste it must be simple. You need to select the graphicItems and add the items in the other scene you want.
    Pseudo code -
    Qt Code:
    1. m_selectedItems = m_source_scene->selectedItems();
    2. // The following slot is called when some action(paste) is triggered.
    3. MyWindow::OnPaste()
    4. {
    5. //m_selectedItems contains a list of selected graphic items
    6. foreach(item,m_selectedItems)
    7. m_target_scene->add(item);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Also can we know where do u want to copy-paste from ? u want copy paste in the same application or from one application to other ?

    Hope this helps

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

    Default Re: QGraphicsView Copy and Paste

    Ok, yet another solution

    Provide a serialization mechanism for your items using QDataStream (or some other custom mechanism, like converting the item to XML representation). When the copy is performed, serialize the item into the mimedata object. When you paste - deserialize.

    Remember that if your item can have child items, they need to be serialized with their parent as well so that pasting the item will paste children as well.

    Such solution will be valid for pasting into the same or another application (contrary to operating on pointers). And it will respect undo/redo properly, if you have one. Pointers probably won't.

  5. #5
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: QGraphicsView Copy and Paste

    Thanks for the replies. I am trying to copy from a scene, so it is probably correct that I should have referred to a scene, not a view. The problem with the first solution, is the assumption that the first scene is still there. If I copy from a scene, then close that window, then paste into another scene, then that breaks.

    I am trying to support both copying and pasting in multiple windows in my app, and into other apps. What I think I should do is put some kind of private data onto the clipboard for my application, and also put an image onto the clipboard for other applications. This will allow another app to paste in an image of my scene, and another scene in my app to paste in the GraphicsItems.

    Wysota, I think that what you are saying about the QDataStream is right, but what I am unfamiliar with is the process of setting up my own mime type, and putting that into the clipboard. I guess what I have to do is to setMimeData in QClipboard with the QDataStream, and use QMimeData to build my mimeData. I think I just have to do some research.
    I think my question was if I was missing something. I don't want to do this a harder way then I have to.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 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 stevel View Post
    Wysota, I think that what you are saying about the QDataStream is right, but what I am unfamiliar with is the process of setting up my own mime type, and putting that into the clipboard.
    You will not get away from that, no matter which solution you choose.

    I guess what I have to do is to setMimeData in QClipboard with the QDataStream, and use QMimeData to build my mimeData.
    No, you have to build your own mime data using QDataStream and then put it in QClipboard using QMimeData.

    I don't want to do this a harder way then I have to.
    Don't worry, it's very easy.

  7. The following user says thank you to wysota for this useful post:

    stevel (25th April 2008)

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

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  10. #9
    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 12:46.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  12. #11
    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

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  14. #13
    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

  15. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,246
    Thanks
    304
    Thanked 866 Times in 853 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)

  16. #15
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.