<?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[GUI very slow when multithreading PyQt]]></title><description><![CDATA[[[topic:post-is-deleted]]]]></description><link>https://forum.qt.io/topic/139519/gui-very-slow-when-multithreading-pyqt</link><generator>RSS for Node</generator><lastBuildDate>Thu, 29 Oct 2026 23:00:43 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/139519.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 23 Sep 2022 10:14:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to GUI very slow when multithreading PyQt on Tue, 27 Sep 2022 05:23:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mayaduchia">@<bdi>mayaduchia</bdi></a> I don't see you doing this: "Also when I used a signal and consider reportProgressxyz as a slot".<br />
And you also ignored my question regarding debugger...</p>
]]></description><link>https://forum.qt.io/post/730284</link><guid isPermaLink="true">https://forum.qt.io/post/730284</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Tue, 27 Sep 2022 05:23:23 GMT</pubDate></item><item><title><![CDATA[Reply to GUI very slow when multithreading PyQt on Mon, 26 Sep 2022 15:59:36 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mayaduchia">@<bdi>mayaduchia</bdi></a></p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> said in <a href="/post/730172">GUI very slow when multithreading PyQt</a>:</p>
<blockquote>
<p dir="auto">What does the debugger tell you?</p>
</blockquote>
]]></description><link>https://forum.qt.io/post/730233</link><guid isPermaLink="true">https://forum.qt.io/post/730233</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 26 Sep 2022 15:59:36 GMT</pubDate></item><item><title><![CDATA[Reply to GUI very slow when multithreading PyQt on Mon, 26 Sep 2022 15:33:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> here's my code:</p>
<pre><code>class Window(QMainWindow):
	 def init(self):
		 #create button for controller
		 controllerOn = QPushButton("Controller On")
		 controllerOn.clicked.connect(self.onControllerOn)
		 #create frame to display plot
		 self.frame = QtWidgets.QFrame()
		 vbox = QVBoxLayout()
		 self.plotter = QtInteractor(self.frame)
		 vbox.addWidget(self.plotter.interactor)
		 
	def onControllerOn(self):
		if(self.controllerFirstConnexion == True):
			#create thread to handle controller inputs
			self.thread = QThread()
			self.worker = Worker()
			self.worker.moveToThread(self.thread)
			#do all the connections
			self.thread.started.connect(self.worker.run)
			self.worker.finished.connect(self.thread.quit)
			self.worker.finished.connect(self.worker.deleteLater)
			self.thread.finished.connect(self.thread.deleteLater)
			#start thread
			self.thread.start()
			
		while(self.worker.controllerConnected == True):
			QApplication.processEvents()
			self.reportProgressxyz(self.worker.x, self.worker.y, self.worker.z)

	def reportProgressxyz(self, x, y, z):
		#update point in plot
		points = np.array([x,y,z])
        self.plotter.add_points(points, render_points_as_spheres=True, point_size=8.0, color ='r')
		
		
class Worker(QObject):
    def run(self):
		pygame.init()
		joysticks = []

		#connect controller
		for i in range(0, pygame.joystick.get_count()):
			joysticks.append(pygame.joystick.Joystick(i))
			joysticks[-1].init()
			
		self.controllerConnected = True
		if pygame.joystick.get_count() == 1:
			while (self.controllerConnected == True) :
			  for event in pygame.event.get():
                      if event.type == pygame.JOYAXISMOTION:
                        if(event.axis == 5):
                            if (event.value&gt;0.9):
                              self.rightTrigger = 1
                            if(event.value&gt;-0.5 and event.value&lt;0.9):
                                self.rightTrigger = 0  
                        if(event.axis == 4):
                            if (event.value&gt;0.9):
                              self.leftTrigger = 1
                            if(event.value&gt;-0.5 and event.value&lt;0.9):
                                self.leftTrigger = 0  
                          
                        while self.rightTrigger == 1 and self.leftTrigger == 0:
                        self.x+=1
                        while self.rightTrigger == 0 and self.leftTrigger == 1:
                        self.y+=1
                        while self.rightTrigger == 1 and self.leftTrigger == 1:
                        self.x+=1
                        self.y+=1
</code></pre>
]]></description><link>https://forum.qt.io/post/730230</link><guid isPermaLink="true">https://forum.qt.io/post/730230</guid><dc:creator><![CDATA[mayaduchia]]></dc:creator><pubDate>Mon, 26 Sep 2022 15:33:23 GMT</pubDate></item><item><title><![CDATA[Reply to GUI very slow when multithreading PyQt on Mon, 26 Sep 2022 11:03:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mayaduchia">@<bdi>mayaduchia</bdi></a> said in <a href="/post/730164">GUI very slow when multithreading PyQt</a>:</p>
<blockquote>
<p dir="auto">the application crashed right away</p>
</blockquote>
<p dir="auto">What does the debugger tell you?<br />
Please post your current code...</p>
]]></description><link>https://forum.qt.io/post/730172</link><guid isPermaLink="true">https://forum.qt.io/post/730172</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Mon, 26 Sep 2022 11:03:10 GMT</pubDate></item><item><title><![CDATA[Reply to GUI very slow when multithreading PyQt on Mon, 26 Sep 2022 10:37:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/simonschroeder">@<bdi>SimonSchroeder</bdi></a> Thank you for your comment. I tried to use the QTime solution and removed QApplication.processEvents() from the while loop, it didn't work, the application crashed right away. Also when I used a signal and consider reportProgressxyz as a slot, for some reason the UI don't get updated.</p>
]]></description><link>https://forum.qt.io/post/730164</link><guid isPermaLink="true">https://forum.qt.io/post/730164</guid><dc:creator><![CDATA[mayaduchia]]></dc:creator><pubDate>Mon, 26 Sep 2022 10:37:02 GMT</pubDate></item><item><title><![CDATA[Reply to GUI very slow when multithreading PyQt on Mon, 26 Sep 2022 09:30:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> There is no Qt Gamepad support on PyQt5</p>
]]></description><link>https://forum.qt.io/post/730156</link><guid isPermaLink="true">https://forum.qt.io/post/730156</guid><dc:creator><![CDATA[mayaduchia]]></dc:creator><pubDate>Mon, 26 Sep 2022 09:30:21 GMT</pubDate></item><item><title><![CDATA[Reply to GUI very slow when multithreading PyQt on Mon, 26 Sep 2022 07:39:25 GMT]]></title><description><![CDATA[<p dir="auto">I don't know Python, but only C++. Maybe someone can help to translate, if necessary.</p>
<p dir="auto">Your problem is your control loop running processEvents(). This has terrible performance (I used to do it in the same way, but notice something like a 100x performance hit). You are most likely correct that <code>reportProgressxyz</code> needs to be called inside the GUI thread. The easiest solution is for your controller thread to emit a signal (from your separate thread) which is connected to a slot in the GUI thread. This just works.</p>
<p dir="auto">You should also make sure that reportProgressxyz is not called to often. This will also slow down the GUI significantly. I personally have the problem with a regular progress bar. What I started adapting is the use of QTime (C++):</p>
<pre><code>QTime time;
time.start();
while(...)
{
  if(time.elapsed() &gt;= 50) // use whatever time fits your case best instead of 50ms
  {
    //reportProgressxyz
    time.restart();
  }
}
</code></pre>
<p dir="auto">50ms is good for my progress bar, but not suitable in your case. 16ms is the time for a single frame at 60Hz. So, maybe do something like 8ms or 4ms.</p>
<p dir="auto">There is another way to execute reportProgressxyz inside the GUI thread from another thread (again C++):</p>
<pre><code>QMetaObject::invokeMethod(qApp, [&amp;](){ this-&gt;reportProgressxyz(worker-&gt;x, worker-&gt;y, worker-&gt;z); });
</code></pre>
<p dir="auto">For performance: never block the GUI thread. Even not when calling processEvents().</p>
]]></description><link>https://forum.qt.io/post/730148</link><guid isPermaLink="true">https://forum.qt.io/post/730148</guid><dc:creator><![CDATA[SimonSchroeder]]></dc:creator><pubDate>Mon, 26 Sep 2022 07:39:25 GMT</pubDate></item><item><title><![CDATA[Reply to GUI very slow when multithreading PyQt on Fri, 23 Sep 2022 18:38:36 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I don't remember whether the module is available with Python but Qt 5 at least had the <a href="https://doc.qt.io/qt-5/qtgamepad-index.html" target="_blank" rel="noopener noreferrer nofollow ugc">Qt Gamepad</a>.</p>
]]></description><link>https://forum.qt.io/post/729990</link><guid isPermaLink="true">https://forum.qt.io/post/729990</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 23 Sep 2022 18:38:36 GMT</pubDate></item><item><title><![CDATA[Reply to GUI very slow when multithreading PyQt on Fri, 23 Sep 2022 12:29:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> Thank you so much for your answer. I will look into it.</p>
]]></description><link>https://forum.qt.io/post/729939</link><guid isPermaLink="true">https://forum.qt.io/post/729939</guid><dc:creator><![CDATA[mayaduchia]]></dc:creator><pubDate>Fri, 23 Sep 2022 12:29:26 GMT</pubDate></item><item><title><![CDATA[Reply to GUI very slow when multithreading PyQt on Fri, 23 Sep 2022 13:08:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mayaduchia">@<bdi>mayaduchia</bdi></a><br />
Usually we basically never call <code>processEvents()</code> for choice, the top-level event loop does the event processing and we leave it to it.  <code>processEvents()</code> can be used in situations where code would block/take a long time, but there are issues and it is not desirable.</p>
<blockquote>
<p dir="auto">And regarding the #get input from controller, it is where I update my GUI (I am changing position in a plot) using the input of the controller at every iteration of the while loop.</p>
</blockquote>
<p dir="auto">Nope!  This loop is running in your non-UI, secondary worker thread.  Qt has an absolute rule that UI operations can <em>only</em> be performed in the main UI thread, else havoc/crash.  You must access the UI directly in any other thread, at most such a thread can emit <em>signals</em> which the main UI thread has a slot on, so that the main thread only performs UI operations in response.</p>
<blockquote>
<p dir="auto">When I integrated the controller module, I created a while loop to handle all controller inputs, but the application crashed. So to prevent the application from crashing, I used the multithreading solution</p>
</blockquote>
<p dir="auto">Truly out of the frying pan but into the fire! ;-)</p>
<p dir="auto">Basically it is a "shame" that you are having to use threads at all.  Half of beginners rush in where angels fear to tread, threading in Qt (and elsewhere!) is about the hardest thing to get right and to be avoided if at all possible, certainly if you are a beginner.</p>
<p dir="auto">You are going to want to start by understanding how your "controller/joystick", and the "pygame library" work and interact.  How are joystick events delivered?  If the pygame stuff is blocking/synchronous, you might run that alone in a thread and have it just emit signals which the main UI thread receives and deals with.</p>
]]></description><link>https://forum.qt.io/post/729929</link><guid isPermaLink="true">https://forum.qt.io/post/729929</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 23 Sep 2022 13:08:10 GMT</pubDate></item><item><title><![CDATA[Reply to GUI very slow when multithreading PyQt on Fri, 23 Sep 2022 10:44:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> thank you for your comment. I added the QApplication.processEvents() because otherwise the while loop would have a delay when updating the reportProgressxyz() function. I don't have any other solution for it (as far as I know). Is there another option that replaces the  QApplication.processEvents()?<br />
And regarding the #get input from controller, it is where I update my GUI (I am changing position in a plot) using the input of the controller at every iteration of the while loop. I don't know if the explanation is clear, if not please do tell me.</p>
]]></description><link>https://forum.qt.io/post/729928</link><guid isPermaLink="true">https://forum.qt.io/post/729928</guid><dc:creator><![CDATA[mayaduchia]]></dc:creator><pubDate>Fri, 23 Sep 2022 10:44:13 GMT</pubDate></item><item><title><![CDATA[Reply to GUI very slow when multithreading PyQt on Fri, 23 Sep 2022 10:25:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mayaduchia">@<bdi>mayaduchia</bdi></a><br />
Hello and welcome.</p>
<p dir="auto">While waiting for someone to tell you how best to write this from Python:</p>
<ul>
<li>
<p dir="auto">Calling <code>QApplication.processEvents()</code> in a tight loop is "costly".  And you never get out of the <code>onControllerOn()</code> slot, your whole UI now runs on the <code>processEvents()</code> instead of allowing it back to the normal, top-level Qt event loop.</p>
</li>
<li>
<p dir="auto">What does your <code>#get input from controller</code> in a tight loop do?  Yes it is in a secondary thread, but if that thread is constantly busy it will impact time for the UI thread.</p>
</li>
</ul>
]]></description><link>https://forum.qt.io/post/729925</link><guid isPermaLink="true">https://forum.qt.io/post/729925</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 23 Sep 2022 10:25:57 GMT</pubDate></item></channel></rss>