<?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[How to seamlessly place item into scene at specific location: Adding QGraphicsItem to scene always places it at 0,0]]></title><description><![CDATA[<p dir="auto">I'm trying to understand how to use QGraphicsScene and QGraphicsView and I am testing item placement with QGraphicsRectItem.   I want to be able to call addRectItem and have the item added to a specific coordinate in the scene, for example (50,50).  However, whenever I add the QRectF(50,50,20,20), it is always placed at (0,0).  I can later move it by simply calling QGraphicsRectItem-&gt;setRect(QRectF(50,50,20,20)) on the returned pointer from addRectItem().  I don't want to do that, I want it placed exactly where it should go the first time.</p>
<p dir="auto">According to the documentation for QGraphicsScene, it says <em>"Items can be placed at any position on the scene".</em>   But in earlier paragraph, it says <em>"...the items position is initialized to (0,0) in the scene".</em>.  So which is it??  And if the latter, how can I get these items to position correctly without the end-user seeing different objects being added to (0,0) and then moved quickly to their new locations?</p>
<p dir="auto">Thanks.</p>
]]></description><link>https://forum.qt.io/topic/106003/how-to-seamlessly-place-item-into-scene-at-specific-location-adding-qgraphicsitem-to-scene-always-places-it-at-0-0</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 20:07:47 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/106003.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 18 Aug 2019 04:55:40 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to seamlessly place item into scene at specific location: Adding QGraphicsItem to scene always places it at 0,0 on Sun, 18 Aug 2019 17:47:13 GMT]]></title><description><![CDATA[<blockquote>
<p dir="auto">There are what, three or four different coordinate systems at play here?</p>
</blockquote>
<p dir="auto">Three. View coordinates, scene coordinates and item coordinates.</p>
<blockquote>
<p dir="auto">I'm coming from a reference point of just dealing with basic painting where (0,0) is always the top-left point</p>
</blockquote>
<p dir="auto">Then what you know translates directly to scene coordinates. On top of that is the view and scene can be moved freely in it so scene <code>0,0</code> and view <code>0,0</code> are not the same. Since you set alignment the scene is moved inside the view so that the top left most item is in the corner. This moves <code>0,0</code> of the scene outside of the view in your case.</p>
<blockquote>
<p dir="auto">Oh, and by the way, scene-&gt;addRect(50,50,20,20) does not draw rectangle at 50,50 like you said, it is drawing it at 0,0, every time</p>
</blockquote>
<p dir="auto">It adds item to scene and in scene coordinates it has position <code>0,0</code>. If you print out <code>item-&gt;pos()</code> it will show <code>0,0</code> as items are always placed in the scene. What you see is the result of alignment that causes the scene to be shifted in view coordinates.</p>
<blockquote>
<p dir="auto">Pictures would really help here</p>
</blockquote>
<p dir="auto">Here you go. Note that those pictures show a view <strong>without alignment</strong> set, to show that scene and view <code>0,0</code> are not the same. The numbers are in scene coordinates.</p>
<p dir="auto">If you do <code>scene-&gt;addRect(50,50,20,20)</code> the result is this:<br />
<img src="https://4b3cma.bl.files.1drv.com/y4mbjxpF-egRb1JLzv_84-tnukFyHTeTijhszmf2wV48rmMUi8Z_JMLbkVv-xxeifSUxXD5fKgXcMBX-UQNOqim12uYMVcmHHhQKfY8RUVkv76MgWyW6g65u-_VcwGoaLkZcvnOiK-39i3ZGDrq4iArTQj2_aaCL4WVvHeNQ1U8ySvPock3GNaAyGTNh2Kj_GlaWWD89ZTzkX1h0fedltIv6g" alt="graphicsscene1" class=" img-fluid img-markdown" /></p>
<p dir="auto">The dotted lines are the <code>0</code> axes of the <strong>scene</strong>. The red point is the item origin point and it's in <code>0,0</code> of the scene. All item transformations are done using that point. You set it via <code>item-&gt;setPos()</code>, <code>item-&gt;setTransform()</code> and the likes. The red rectangle is the relative bounding box of the item. In case of a <code>QGraphicsRectItem</code> it grows to encompass the actual black rectangle visible on the screen. That red rectangle is going to resize as you change the geometry of your rectangle via <code>item-&gt;setRect()</code>. The black box is the rectangle drawn by the item. It is set to have an origin at <code>50,50</code> in item's coordinates, so relative to the red point (item origin).</p>
<p dir="auto">Now, if you do</p>
<pre><code>auto rectItem = new QGraphicsRectItem(0,0,20,20);
rectItem-&gt;setPos(50,50);
scene-&gt;addItem(rectItem);
</code></pre>
<p dir="auto">the result is:<br />
<img src="https://pi0vvg.bl.files.1drv.com/y4m2L2GpprocoPqa4raSH3y2BhM8wDFu7xn9GzkwL_f-o4y2Jeihwj1xAC_ZpedtA3aLjeQcIe2q6xxK0IBZGDSlctH0LWIpywv1PZZnCcn_5poj3ZLvwrcxToKme8gSD9RNWRVIinXZ7hyQX_diFKscPASjgd8gqzeVwPlVubm8IFl9Xlgvs0DNvhuVH8_d00XinjsuSuTQnzBwk4SXR2xZw" alt="graphicsitem2" class=" img-fluid img-markdown" /></p>
<p dir="auto">This creates an item, again in <code>0,0</code> in scene coordinates then moves it to <code>50,50</code>. The item itself draws a black rectangle at <code>0,0</code> in item coordinates.</p>
]]></description><link>https://forum.qt.io/post/546678</link><guid isPermaLink="true">https://forum.qt.io/post/546678</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Sun, 18 Aug 2019 17:47:13 GMT</pubDate></item><item><title><![CDATA[Reply to How to seamlessly place item into scene at specific location: Adding QGraphicsItem to scene always places it at 0,0 on Sun, 18 Aug 2019 16:45:17 GMT]]></title><description><![CDATA[<p dir="auto">Brain hurt</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> , I understand what you are saying about docs, I did read them before I asked here, but yes, I don't understand everything thus asking questions... :-)</p>
<p dir="auto">There are what, three or four different coordinate systems at play here?  Does anybody have any pictures that clearly show how these all tie together and how to place an object where you want it?  I'm coming from a reference point of just dealing with basic painting where (0,0) is always the top-left point and not relative to anything.  I'd go back to basic painting, but I've found my app concept really needs to use QGraphicsView and QGraphicsScene.</p>
<p dir="auto">Oh, and by the way, scene-&gt;addRect(50,50,20,20) does not draw rectangle at 50,50 like you said, it is drawing it at 0,0, every time, and just to be clear, when I say (0,0), visually, I mean the top-left corner of the QGraphicsView.  Maybe I should point out that I have QGraphicsView alignment in the form editor set for "AlignLeft, AlignTop".  I changed it that way because I don't want everything centered in the view.</p>
<p dir="auto">Pictures would really help here, not finding many pics in Qt docs that help  explainl, if they are there, they are spread out in hidy-holes.</p>
]]></description><link>https://forum.qt.io/post/546673</link><guid isPermaLink="true">https://forum.qt.io/post/546673</guid><dc:creator><![CDATA[Snorkelbuckle]]></dc:creator><pubDate>Sun, 18 Aug 2019 16:45:17 GMT</pubDate></item><item><title><![CDATA[Reply to How to seamlessly place item into scene at specific location: Adding QGraphicsItem to scene always places it at 0,0 on Sun, 18 Aug 2019 08:07:11 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
Adding to <a class="plugin-mentions-user plugin-mentions-a" href="/user/chris-kawa">@<bdi>Chris-Kawa</bdi></a> , its very important to read<br />
<a href="https://doc.qt.io/qt-5/graphicsview.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/graphicsview.html</a><br />
section The Graphics View Coordinate System / Item Coordinates<br />
to understand how it works together.</p>
]]></description><link>https://forum.qt.io/post/546630</link><guid isPermaLink="true">https://forum.qt.io/post/546630</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 18 Aug 2019 08:07:11 GMT</pubDate></item><item><title><![CDATA[Reply to How to seamlessly place item into scene at specific location: Adding QGraphicsItem to scene always places it at 0,0 on Sun, 18 Aug 2019 07:16:34 GMT]]></title><description><![CDATA[<p dir="auto">You misunderstood. <code>scene-&gt;addRect(50,50,20,20)</code> does not add an item at position <code>50,50</code>. It adds an item at position <code>0,0</code> that draws a rectangle at <code>50,50</code>, so your entire item bounding rectangle is <code>0,0,70,70</code>.</p>
<p dir="auto">To add an item at <code>50,50</code> you would do:</p>
<pre><code>auto rectItem = new QGraphicsRectItem(0,0,20,20);
rectItem-&gt;setPos(50,50);
scene-&gt;addItem(rectItem);
</code></pre>
<p dir="auto"><code>item-&gt;setPos()</code> does not move the rectangle within the item, it moves the item itself. <code>item-&gt;setRect()</code> on the other hand moves the rectangle inside the item and does not move the item itself.</p>
<p dir="auto">In other words rectangle coordinates are relative to the item origin point, not to <code>0,0</code> of the scene.</p>
]]></description><link>https://forum.qt.io/post/546625</link><guid isPermaLink="true">https://forum.qt.io/post/546625</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Sun, 18 Aug 2019 07:16:34 GMT</pubDate></item></channel></rss>