PyQt5 - Emit once, Slot called multiple times, slowing down the application
-
Apologies in advance for not being able to provide all of the necessary code, as it is a private repo. I'm going to try my best to explain my approach and the bug using very limited code.
I have a
MainWindow()
class and aCustomizeGraphs()
class. TheCustomizeGraphs()
class is for a Child Window that appears when a button, called Customize Graphs, in theMainWindow()
is clicked.I need to send back a list from the
CustomizeGraphs()
class to theMainWindow()
and so I'm using signals and slots.Here's the basic structure of the code.
class MainWindow(): def __init__(): # Init Code def handlePlot(self, checked): window.signal.connect(self.handleData) def handleData(self, data): # Other function calls self.initPlot(data) # initPlot() only creates and/or refreshes PlotWidgets() class CustomizeGraphs(): plotSignal = pyqtsignal(list) def handleOKBtn(self): data = [ # Number of plots is sent in this array along with other data ] plotSignal.emit(data) QTimer.singleShot(0, self.close) # To close the child window
Now here's what the bug is:
- The first time I open the child window and select the number of plots I want to display, say 3, and click on the OK button,
emit
is called once, and so thehandleData()
function ONCE, as it should. - Now if I click on the Customize Graph button again, and open the Child Window and select the same/different value, and click on OK,
emit
is still called once, and yet this timehandleData()
is called twice.
This pattern continues, and it slows down the applications performance every time I make changes to the graphs.
Why is this happening? Has anyone faced this before? Please let me know. Thank you.
- The first time I open the child window and select the number of plots I want to display, say 3, and click on the OK button,
-
@AlexJMercer24K
You do not show where you callhandlePlot()
from. Each time you call it it will callwindow.signal.connect(self.handleData)
and that will create a new, additional connection toself.handleData
. My guess is you call it more than once, so you end up with the signal connected multiple times toself.handleData
.Either don't do that, count how many times you have done the connect or use the "unique connection" parameter to
connect()
(however you do that in Python) so that it does not create duplicate connections. -
It looks like your signal-slot connection is stacking each time you open the child window. Try disconnecting the signal before reconnecting it in handlePlot() using window.signal.disconnect(self.handleData). This should prevent multiple calls to handleData(). My cousin, who is a professional photographer, was always struggling with sending full-resolution images to clients. After trying various platforms, he finally settled on Filemail, and it has been a game-changer for him. No compression, no sign-ups for recipients, and super-fast uploads—everything he needed for hassle-free file transfers.