<?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[Mouse handling using asyncio?]]></title><description><![CDATA[<p dir="auto">So I'm writing a mouse class that you call when you want to drag and draw a selection area on the screen. The idea is that the calling function calls areaSelect, then waits for areaSelect to return a Rect() of the location of the dragged rectangle. I have written a very basic outline of what the class will look like. Right now it hooks into the mouse events, sets a bool to True, then waits forever for the mouse release. I was wondering if there is a more elegant way than an endless While, like using async or something, to wait for the bool to be set to False.</p>
<pre><code>from PySide6.QtWidgets import QWidget
from PySide6.QtCore import QRect

class MouseHandler(QWidget):
    def __init__(self):
        super().__init__()
        self.drag_in_progress = False

    def mousePressEvent(self, event):
        if self.drag_in_progress is True:
            # do something
            pass

    def mouseMoveEvent(self, event):
        if self.drag_in_progress is True:
            # do something
            pass

    def mouseReleaseEvent(self, event):
        if self.drag_in_progress is True:
            # do something
            self.drag_in_progress = False

    def areaSelect(self) -&gt; QRect:
        self.drag_in_progress = True
        while self.drag_in_progress is not False:
            pass
        return QRect(0, 0, 1, 1)

</code></pre>
]]></description><link>https://forum.qt.io/topic/157144/mouse-handling-using-asyncio</link><generator>RSS for Node</generator><lastBuildDate>Thu, 23 Apr 2026 23:35:17 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/157144.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 08 Jun 2024 20:10:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Mouse handling using asyncio? on Sat, 08 Jun 2024 23:49:36 GMT]]></title><description><![CDATA[<p dir="auto">I just realized that this idea won't work. You can't just have one event handler halt the whole GUI while waiting for a rectangle to be drawn. Back to the drawing board.</p>
]]></description><link>https://forum.qt.io/post/802179</link><guid isPermaLink="true">https://forum.qt.io/post/802179</guid><dc:creator><![CDATA[dullfig]]></dc:creator><pubDate>Sat, 08 Jun 2024 23:49:36 GMT</pubDate></item><item><title><![CDATA[Reply to Mouse handling using asyncio? on Mon, 10 Jun 2024 08:46:37 GMT]]></title><description><![CDATA[<p dir="auto">Well, there <em>is</em> a way to achieve what you want to do. Still, it is better not to do it this way. Instead of your infinite loop you can create QEventLoop and call <code>exec()</code> on it. On mouseReleaseEvent you can then quit the local event loop. Be careful with this if you really want to go down this route... (nested event loops can be nasty!)</p>
]]></description><link>https://forum.qt.io/post/802247</link><guid isPermaLink="true">https://forum.qt.io/post/802247</guid><dc:creator><![CDATA[SimonSchroeder]]></dc:creator><pubDate>Mon, 10 Jun 2024 08:46:37 GMT</pubDate></item><item><title><![CDATA[Reply to Mouse handling using asyncio? on Sun, 09 Jun 2024 18:37:30 GMT]]></title><description><![CDATA[<p dir="auto">yup. thanks</p>
]]></description><link>https://forum.qt.io/post/802217</link><guid isPermaLink="true">https://forum.qt.io/post/802217</guid><dc:creator><![CDATA[dullfig]]></dc:creator><pubDate>Sun, 09 Jun 2024 18:37:30 GMT</pubDate></item><item><title><![CDATA[Reply to Mouse handling using asyncio? on Sun, 09 Jun 2024 08:36:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dullfig">@<bdi>dullfig</bdi></a><br />
But that is exactly what I said!  You must not "wait" for anything.  And you don't need to.  You don't have to "go back to the drawing board", you just need to do your work from <code>mouseReleaseEvent()</code>, as I wrote earlier either directly or via emitting a signal.  I don't see the problem.</p>
]]></description><link>https://forum.qt.io/post/802186</link><guid isPermaLink="true">https://forum.qt.io/post/802186</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 09 Jun 2024 08:36:21 GMT</pubDate></item><item><title><![CDATA[Reply to Mouse handling using asyncio? on Sat, 08 Jun 2024 23:49:36 GMT]]></title><description><![CDATA[<p dir="auto">I just realized that this idea won't work. You can't just have one event handler halt the whole GUI while waiting for a rectangle to be drawn. Back to the drawing board.</p>
]]></description><link>https://forum.qt.io/post/802179</link><guid isPermaLink="true">https://forum.qt.io/post/802179</guid><dc:creator><![CDATA[dullfig]]></dc:creator><pubDate>Sat, 08 Jun 2024 23:49:36 GMT</pubDate></item><item><title><![CDATA[Reply to Mouse handling using asyncio? on Sat, 08 Jun 2024 21:35:03 GMT]]></title><description><![CDATA[<p dir="auto">If you want to draw the selecting band see <a href="https://doc.qt.io/qt-6/qrubberband.html" target="_blank" rel="noopener noreferrer nofollow ugc">QRubberBand</a>.<br />
If you were using a <code>GraphicsScene</code> that has rubberband drawing and selection.  I don't know about <code>QWidget</code>, what you are doing there, what your zooming is about, etc.<br />
Qt also has various item-selection mechanisms if that is what you want.<br />
If you want a "more generalized way of doing it" have your <code>mouseReleaseEvent()</code> emit a <em>signal</em> for the area selection or whatever.<br />
It is "unusual" to declare a class deriving from <code>QWidget</code> to be a <code>MouseHandler</code> in any generic sense.  I don't know how you think you are going to use your class.</p>
]]></description><link>https://forum.qt.io/post/802173</link><guid isPermaLink="true">https://forum.qt.io/post/802173</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 08 Jun 2024 21:35:03 GMT</pubDate></item><item><title><![CDATA[Reply to Mouse handling using asyncio? on Sat, 08 Jun 2024 20:34:23 GMT]]></title><description><![CDATA[<p dir="auto">two things:</p>
<p dir="auto">my code already does a "zoom to area" thing that does get handled in the mouseRelease event, but I wanted to come up with a more generalized way of doing it, as I can see "drag and click" to come in handy in other parts of my program. And yes, if I call this function from inside another event handler, then the mouse clicks will never even be seen.</p>
<p dir="auto">you mentioned "other ways to do this". can you point me in that direction?</p>
]]></description><link>https://forum.qt.io/post/802170</link><guid isPermaLink="true">https://forum.qt.io/post/802170</guid><dc:creator><![CDATA[dullfig]]></dc:creator><pubDate>Sat, 08 Jun 2024 20:34:23 GMT</pubDate></item><item><title><![CDATA[Reply to Mouse handling using asyncio? on Sat, 08 Jun 2024 20:26:50 GMT]]></title><description><![CDATA[<p dir="auto">Leaving aside whether there are already other ways to do this.</p>
<p dir="auto">The point about event-driven UI coding is that you don't write or call a function like your <code>areaSelect()</code>, which blocks till something happens.  Instead in your <code>mouseReleaseEvent()</code> you recognise/calculate the dragged rectangle and execute whatever you want to do with it from there, either directly or via Qt's signal/slot system so you can write the handler code elsewhere.</p>
]]></description><link>https://forum.qt.io/post/802169</link><guid isPermaLink="true">https://forum.qt.io/post/802169</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 08 Jun 2024 20:26:50 GMT</pubDate></item></channel></rss>