Results 1 to 7 of 7

Thread: Copy and Paste of QGraphicsItems in a Scene

  1. #1
    Join Date
    Apr 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Copy and Paste of QGraphicsItems in a Scene

    How do i Copy the QGraphicsitems and paste in the same Scene

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Copy and Paste of QGraphicsItems in a Scene

    You use QClipboard where you serialize your item on copy and then deserialize it on paste. You have to provide your own serialization mechanism, for example using QDataStream.
    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
    Apr 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Copy and Paste of QGraphicsItems in a Scene

    is it possible to use like this in the DataStream ?

    Qt Code:
    1. QList<QGraphicsItem*> itemToCopy = scene->selectedItems();
    To copy to clipboard, switch view to plain text mode 

    and how do i make a copy of selected different graphicalitems ?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Copy and Paste of QGraphicsItems in a Scene

    There is no magic box that serializes your custom items and makes copies of them. You have to provide a mechanism that uses some of the architectures already present in Qt (such as QDataStream or XML) to store all relevant data for your item (like the position, size, item type, outline colour, brush colour, etc.) that is required to recreate the item. Then you have to store that data in a drag and recreate the item back from it when it is pasted.
    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
    Jul 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Copy and Paste of QGraphicsItems in a Scene

    is there no easy way without using this qdatastrem. if not and this is the only way then please elaborate it .

  6. #6
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Copy and Paste of QGraphicsItems in a Scene

    Here is one example how I save items in an xml project file
    Qt Code:
    1. xmlWriter.writeStartElement("Widget");
    2. xmlWriter.writeAttribute("id", currentProxy->accessibleName());
    3. xmlWriter.writeAttribute("X", QString::number(item->scenePos().x()));
    4. xmlWriter.writeAttribute("Y", QString::number(item->scenePos().y()));
    5. xmlWriter.writeAttribute("Z", QString::number(item->zValue()));
    6. xmlWriter.writeAttribute("Width", QString::number(progressBar->size().width()));
    7. xmlWriter.writeAttribute("Height", QString::number(progressBar->size().height()));
    8. xmlWriter.writeAttribute("Min", QString::number(progressBar->getMin()));
    9. xmlWriter.writeAttribute("Max", QString::number(progressBar->getMax()));
    10. xmlWriter.writeAttribute("Orientation", progressBar->getOrientation());
    11. xmlWriter.writeAttribute("Color", progressBar->getColor());
    12. xmlWriter.writeAttribute("BackgroundColor", progressBar->getBackgroundColor());
    13. xmlWriter.writeAttribute("BorderSize", QString::number(progressBar->getBorderSize()));
    14. xmlWriter.writeAttribute("Function", progressBar->getFunction());
    15. xmlWriter.writeEndElement();
    To copy to clipboard, switch view to plain text mode 

    and the code for reading it back
    Qt Code:
    1. QStringRef xValue = attributes.value("X");
    2. QStringRef yValue = attributes.value("Y");
    3. QStringRef zValue = attributes.value("Z");
    4. QStringRef heightValue = attributes.value("Height");
    5. QStringRef widthValue = attributes.value("Width");
    6. QStringRef minValue = attributes.value("Min");
    7. QStringRef maxValue = attributes.value("Max");
    8. QStringRef orientation = attributes.value("Orientation");
    9. QStringRef color = attributes.value("Color");
    10. QStringRef backgroundColor = attributes.value("BackgroundColor");
    11. QStringRef borderSize = attributes.value("BorderSize");
    12. QStringRef function = attributes.value("Function");
    13.  
    14. ExampleInstrument *exampleInstrument = new ExampleInstrument();
    15. exampleInstrument->setMin(minValue.toString().toInt());
    16. exampleInstrument->setMax(maxValue.toString().toInt());
    17. exampleInstrument->setOrientation(orientation.toString());
    18. exampleInstrument->setColor(color.toString());
    19. exampleInstrument->setBackgroundColor(backgroundColor.toString());
    20. exampleInstrument->setBorderSize(borderSize.toString().toInt());
    21. exampleInstrument->setFunction(function.toString());
    22.  
    23. CdProxy *proxy = new CdProxy();
    24. proxy->setWidget(exampleInstrument);
    25. proxy->setPos(xValue.toString().toFloat(), yValue.toString().toFloat());
    26. proxy->setZValue(zValue.toString().toFloat());
    27. proxy->resize(widthValue.toString().toFloat(), heightValue.toString().toFloat());
    28. proxy->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
    To copy to clipboard, switch view to plain text mode 

    So what you need to do to copy items is to save the data needed and then create a new object with that data.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Copy and Paste of QGraphicsItems in a Scene

    Quote Originally Posted by gaurav6005 View Post
    is there no easy way without using this qdatastrem. if not and this is the only way then please elaborate it .
    There are plenty of ways but you need to know what you want. There are no ready solutions for storing arbitrary data from arbitrary objects. You will have to write the serializing procedure yourself.
    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.


Similar Threads

  1. Replies: 1
    Last Post: 27th November 2014, 09:11
  2. QGraphicsView Copy and Paste
    By stevel in forum Qt Programming
    Replies: 14
    Last Post: 13th January 2014, 07:57
  3. How to copy from Excel and paste to QTableView
    By sms in forum Qt Programming
    Replies: 5
    Last Post: 7th February 2009, 03:58
  4. QTextEdit - copy and paste problems in X11
    By chezifresh in forum Qt Programming
    Replies: 2
    Last Post: 30th June 2008, 19:21
  5. Copy / Paste doesn't work with LineEdit
    By ia32 in forum Qt Tools
    Replies: 2
    Last Post: 5th May 2008, 21:44

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.