<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Copy and paste QGraphicItem on my QGraphicScene]]></title><description><![CDATA[<p dir="auto">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</p>
<p dir="auto">QList&lt;QGraphicsItem *&gt; copiedItem = this-&gt;selectedItems();</p>
<p dir="auto">All i want to finally do is, i wanted to copy and paste QGraphicItem on my QGraphicScene .<br />
thanks in advance</p>
]]></description><link>https://forum.qt.io/topic/36243/copy-and-paste-qgraphicitem-on-my-qgraphicscene</link><generator>RSS for Node</generator><lastBuildDate>Sun, 10 May 2026 15:39:03 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/36243.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 06 Jan 2014 09:40:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Copy and paste QGraphicItem on my QGraphicScene on Wed, 08 Jan 2014 13:36:49 GMT]]></title><description><![CDATA[<p dir="auto">Hi i am new to Qt, and i am working on cut, copy, paste plz give directions how to solve this</p>
<p dir="auto">This is how i copy an item and serialize it</p>
<p dir="auto">[code]QFile file("fileName.dat");<br />
file.open(QIODevice::WriteOnly);<br />
QDataStream out(&amp;file);</p>
<p dir="auto">QList&lt;QGraphicsItem <em>&gt; list1= this-&gt;selectedItems();<br />
itemListSize = list1.size();<br />
out&lt;&lt;itemListSize;<br />
foreach( QGraphicsItem</em> item, list1)<br />
{<br />
out &lt;&lt; item-&gt;x();<br />
out &lt;&lt; item-&gt;y();<br />
}<br />
[/code]</p>
<p dir="auto">and for pasting it back i use the following code</p>
<p dir="auto">[code]QFile file("fileName.dat");<br />
QList&lt;QGraphicsItem *&gt; list1 =  this-&gt;selectedItems();<br />
file.open(QIODevice::ReadOnly);<br />
QDataStream in(&amp;file);<br />
in&gt;&gt;itemListSize;<br />
foreach(QGraphicsItem *item, list1)<br />
{<br />
in &gt;&gt; item-&gt;x();<br />
in &gt;&gt; item-&gt;y();<br />
scene1-&gt;addItem(item);<br />
}</p>
<p dir="auto">file.close();[/code]</p>
<p dir="auto">but i cannot get the items x and y position while reading data. Am i serializing in the correct way, plz help me out.<br />
And this is how i  call the rect item using a mousePressEvent from my DiagramItem class</p>
<p dir="auto">[code]{<br />
if (mouseEvent-&gt;button() != Qt::LeftButton)<br />
return;</p>
<pre><code> DiagramItem *item;
 switch (myMode) {
</code></pre>
<p dir="auto">case InsertItem:<br />
item = new DiagramItem(myItemType);<br />
item-&gt;setBrush(myItemColor);<br />
addItem(item);<br />
item-&gt;setPos(mouseEvent-&gt;scenePos());<br />
emit itemInserted(item);<br />
break;</p>
<pre><code>   ;  
 }

 QGraphicsScene::mousePressEvent(mouseEvent);
</code></pre>
<p dir="auto">}<br />
[/code]</p>
<p dir="auto">This is the code written for creating a polygon item in my DiagramItem's class constructor</p>
<p dir="auto">[code]myDiagramType = diagramType;<br />
QPainterPath path;<br />
switch (myDiagramType) {<br />
case Step:<br />
myPolygon &lt;&lt; QPointF(-100, -100) &lt;&lt; QPointF(100, -100)<br />
&lt;&lt; QPointF(100, 100) &lt;&lt; QPointF(-100, 100)<br />
&lt;&lt; QPointF(-100, -100);<br />
}<br />
setPolygon(myPolygon);</p>
<pre><code> setFlag(QGraphicsItem::ItemIsMovable, true);
 setFlag(QGraphicsItem::ItemIsSelectable, true);
 setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
</code></pre>
<p dir="auto">}[/code]</p>
<p dir="auto">if suggested any changes that has to be done in the code will be much helpfull ;(</p>
]]></description><link>https://forum.qt.io/post/209900</link><guid isPermaLink="true">https://forum.qt.io/post/209900</guid><dc:creator><![CDATA[vinzzz]]></dc:creator><pubDate>Wed, 08 Jan 2014 13:36:49 GMT</pubDate></item></channel></rss>