<?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[Restricting a movable QGraphicsRectItem in the Scene Rect]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I have a subclass of QGraphicsRectItem that is basically a rectangle that is user-movable and gets resized automatically by the application when needed.</p>
<p dir="auto">The rectangle starts out being the same size of the whole view, but when it shrinks (only horizontally) it has space to move and it moves fine. What I am trying to do is keep the item inside the scene rectangle. The scene rectangle is always the same size as the view's viewport so basically I am trying to be able to move the rectangle freely but if one of the sides hits the viewport's wall it should not be able to move further, as to never lose sight of a piece of the rectangle item. Also stop the rectangle from growing bigger than the viewport's bounding rectangle so that when the rect item covers the whole viewport it cannot be moved.</p>
<p dir="auto">For this I found in the docs the way to do it is re-implementing QGraphicsRectItem::itemChange() in my class. So I did:</p>
<p dir="auto">@<br />
QVariant ReferenceRect::itemChange(GraphicsItemChange change, const QVariant &amp;value)<br />
{<br />
if(change == ItemPositionChange &amp;&amp; scene())<br />
{<br />
QPointF newPos = value.toPointF();<br />
QRectF r = scene()-&gt;sceneRect();</p>
<pre><code>    if(!r.contains(newPos))
    {
        newPos.setX(qMin(r.right(), qMax(newPos.x(), r.left())));
        newPos.setY(qMin(r.bottom(), qMax(newPos.y(), r.top())));
        return newPos;
    }
}

return QGraphicsRectItem::itemChange(change, value);
</code></pre>
<p dir="auto">}<br />
@</p>
<p dir="auto">This does the job fine for the left hand boundaries and for the top boundaries, but I can still move the rectangle out of view to the right.</p>
<p dir="auto">So how could I restrict this to also the left hand boundary? Also I'm wondering, above the newPos of my Rect item, is it the top left-corner of the rectangle??</p>
<p dir="auto">I appreciate any tips, tricks or info of any kind!<br />
Thanks.</p>
]]></description><link>https://forum.qt.io/topic/11660/restricting-a-movable-qgraphicsrectitem-in-the-scene-rect</link><generator>RSS for Node</generator><lastBuildDate>Sat, 07 Nov 2026 04:31:12 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/11660.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 25 Nov 2011 11:18:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Restricting a movable QGraphicsRectItem in the Scene Rect on Fri, 18 Jan 2019 13:53:24 GMT]]></title><description><![CDATA[<p dir="auto">Can anyone please share code for custom QgraphicsItem.<br />
right now i am facing same issue "QGraphicsItem is moving out of scene" when i am doing mouse move for graphics item</p>
]]></description><link>https://forum.qt.io/post/505696</link><guid isPermaLink="true">https://forum.qt.io/post/505696</guid><dc:creator><![CDATA[Ramakrushna]]></dc:creator><pubDate>Fri, 18 Jan 2019 13:53:24 GMT</pubDate></item><item><title><![CDATA[Reply to Restricting a movable QGraphicsRectItem in the Scene Rect on Sun, 06 Jul 2014 05:14:12 GMT]]></title><description><![CDATA[<p dir="auto">I fixed it. So don't need your code anymore.:)</p>
]]></description><link>https://forum.qt.io/post/126661</link><guid isPermaLink="true">https://forum.qt.io/post/126661</guid><dc:creator><![CDATA[loading...]]></dc:creator><pubDate>Sun, 06 Jul 2014 05:14:12 GMT</pubDate></item><item><title><![CDATA[Reply to Restricting a movable QGraphicsRectItem in the Scene Rect on Sun, 06 Jul 2014 03:07:22 GMT]]></title><description><![CDATA[<p dir="auto">[quote author="xtraction" date="1322238215"] yes thanks!,  just added another 'if' with another point nPos taking into account the size of the rectangle and did the same thing and it works fine.</p>
<p dir="auto">@<br />
QPointF nPos(newPos.x() + rect().width(), newPos.y() + rect().height());<br />
@<br />
[/quote]</p>
<p dir="auto">Hi xtraction!<br />
Can you share your code of QgraphicsItem? I tried to use the same code but i dont know why i can still move the rectangle out of the scene. Thanks!</p>
]]></description><link>https://forum.qt.io/post/126660</link><guid isPermaLink="true">https://forum.qt.io/post/126660</guid><dc:creator><![CDATA[loading...]]></dc:creator><pubDate>Sun, 06 Jul 2014 03:07:22 GMT</pubDate></item><item><title><![CDATA[Reply to Restricting a movable QGraphicsRectItem in the Scene Rect on Fri, 25 Nov 2011 16:23:35 GMT]]></title><description><![CDATA[<p dir="auto">yes thanks!,  just added another 'if' with another point nPos taking into account the size of the rectangle and did the same thing and it works fine.</p>
<p dir="auto">@<br />
QPointF nPos(newPos.x() + rect().width(), newPos.y() + rect().height());<br />
@</p>
]]></description><link>https://forum.qt.io/post/118252</link><guid isPermaLink="true">https://forum.qt.io/post/118252</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 25 Nov 2011 16:23:35 GMT</pubDate></item><item><title><![CDATA[Reply to Restricting a movable QGraphicsRectItem in the Scene Rect on Fri, 25 Nov 2011 12:00:41 GMT]]></title><description><![CDATA[<p dir="auto">You have to take into account not only the position, but also the size of the item, i.e. the rect().width() and rect().height().</p>
]]></description><link>https://forum.qt.io/post/118223</link><guid isPermaLink="true">https://forum.qt.io/post/118223</guid><dc:creator><![CDATA[ludde]]></dc:creator><pubDate>Fri, 25 Nov 2011 12:00:41 GMT</pubDate></item></channel></rss>