Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PyQt5 - Emit once, Slot called multiple times, slowing down the application
Forum Updated to NodeBB v4.3 + New Features

PyQt5 - Emit once, Slot called multiple times, slowing down the application

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 3 Posters 201 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AlexJMercer24K
    wrote on 2 Feb 2025, 13:07 last edited by
    #1

    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 a CustomizeGraphs() class. The CustomizeGraphs() class is for a Child Window that appears when a button, called Customize Graphs, in the MainWindow() is clicked.

    I need to send back a list from the CustomizeGraphs() class to the MainWindow() 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 the handleData() 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 time handleData() 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.

    J 1 Reply Last reply 2 Feb 2025, 13:54
    0
    • A AlexJMercer24K
      2 Feb 2025, 13:07

      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 a CustomizeGraphs() class. The CustomizeGraphs() class is for a Child Window that appears when a button, called Customize Graphs, in the MainWindow() is clicked.

      I need to send back a list from the CustomizeGraphs() class to the MainWindow() 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 the handleData() 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 time handleData() 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.

      J Offline
      J Offline
      JonB
      wrote on 2 Feb 2025, 13:54 last edited by
      #2

      @AlexJMercer24K
      You do not show where you call handlePlot() from. Each time you call it it will call window.signal.connect(self.handleData) and that will create a new, additional connection to self.handleData. My guess is you call it more than once, so you end up with the signal connected multiple times to self.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.

      1 Reply Last reply
      1
      • C Offline
        C Offline
        CallowayHampton
        wrote on 24 Feb 2025, 14:28 last edited by CallowayHampton
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved