PDA

View Full Version : Copy and Paste of QGraphicsItems in a Scene



subash
24th April 2009, 21:27
How do i Copy the QGraphicsitems and paste in the same Scene

wysota
25th April 2009, 08:05
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.

subash
25th April 2009, 10:10
is it possible to use like this in the DataStream ?



QList<QGraphicsItem*> itemToCopy = scene->selectedItems();


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

wysota
27th April 2009, 20:22
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.

gaurav6005
6th July 2011, 13:12
is there no easy way without using this qdatastrem. if not and this is the only way then please elaborate it .

meazza
6th July 2011, 13:42
Here is one example how I save items in an xml project file

xmlWriter.writeStartElement("Widget");
xmlWriter.writeAttribute("id", currentProxy->accessibleName());
xmlWriter.writeAttribute("X", QString::number(item->scenePos().x()));
xmlWriter.writeAttribute("Y", QString::number(item->scenePos().y()));
xmlWriter.writeAttribute("Z", QString::number(item->zValue()));
xmlWriter.writeAttribute("Width", QString::number(progressBar->size().width()));
xmlWriter.writeAttribute("Height", QString::number(progressBar->size().height()));
xmlWriter.writeAttribute("Min", QString::number(progressBar->getMin()));
xmlWriter.writeAttribute("Max", QString::number(progressBar->getMax()));
xmlWriter.writeAttribute("Orientation", progressBar->getOrientation());
xmlWriter.writeAttribute("Color", progressBar->getColor());
xmlWriter.writeAttribute("BackgroundColor", progressBar->getBackgroundColor());
xmlWriter.writeAttribute("BorderSize", QString::number(progressBar->getBorderSize()));
xmlWriter.writeAttribute("Function", progressBar->getFunction());
xmlWriter.writeEndElement();

and the code for reading it back

QStringRef xValue = attributes.value("X");
QStringRef yValue = attributes.value("Y");
QStringRef zValue = attributes.value("Z");
QStringRef heightValue = attributes.value("Height");
QStringRef widthValue = attributes.value("Width");
QStringRef minValue = attributes.value("Min");
QStringRef maxValue = attributes.value("Max");
QStringRef orientation = attributes.value("Orientation");
QStringRef color = attributes.value("Color");
QStringRef backgroundColor = attributes.value("BackgroundColor");
QStringRef borderSize = attributes.value("BorderSize");
QStringRef function = attributes.value("Function");

ExampleInstrument *exampleInstrument = new ExampleInstrument();
exampleInstrument->setMin(minValue.toString().toInt());
exampleInstrument->setMax(maxValue.toString().toInt());
exampleInstrument->setOrientation(orientation.toString());
exampleInstrument->setColor(color.toString());
exampleInstrument->setBackgroundColor(backgroundColor.toString());
exampleInstrument->setBorderSize(borderSize.toString().toInt());
exampleInstrument->setFunction(function.toString());

CdProxy *proxy = new CdProxy();
proxy->setWidget(exampleInstrument);
proxy->setPos(xValue.toString().toFloat(), yValue.toString().toFloat());
proxy->setZValue(zValue.toString().toFloat());
proxy->resize(widthValue.toString().toFloat(), heightValue.toString().toFloat());
proxy->setCacheMode(QGraphicsItem::DeviceCoordinateCache) ;

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

wysota
6th July 2011, 16:15
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.