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. Combo Box calling slot without connect
Forum Updated to NodeBB v4.3 + New Features

Combo Box calling slot without connect

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 542 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.
  • T Offline
    T Offline
    teddy.ispsystem
    wrote on last edited by
    #1

    Hello every one,

    I am working on python (3.8.1) gui using PyQt5 (5.12.3). I am working on windows 10. To build my gui I used QtDesigner (on QtCreator 4.13.3) and I convert it using pyuic5. If you want to reproduce the issue I placed all the file here: https://www.transfernow.net/dl/20210429OllW9k9q

    Files :

    • cmdExport.bat : command used to convert the .ui file
    • test.ui : File created with QtCreator using QtDesigner
    • application.py : File containing my program
    • test.py : File generated by the cmdExport.bat

    So here is my problem :

    I create a class which instantiate the gui generated by pyuic5

    class application(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            # Load the design file
            super(application, self).__init__()
            # QtWidgets.QMainWindow.__init__(self, parent)
            self.ui = guiTest.Ui_MainWindow()
            self.ui.setupUi(self)
            
            self.ui.comboBox.activated.connect(self.on_comboBox_activated)
            
        def on_comboBox_activated(self, value):
            print('== Enter in on_comboBox_activated ==')
            print('value = {0}'.format(value))
            print('== Leave on_comboBox_activated ==')
    

    The problem is when I clicked on the combo box the slot on_comboBox_activated is called 3 times. If I comment the line :

    self.ui.comboBox.activated.connect(self.on_comboBox_activated)
    

    The slot is also called but 2 times. It seems that if I call a function on_comboBox_activated on the class which instantiate the gui it will be called somewhere. To "solve" the problem I have to change the name of the function in my class like that :

    class application(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            # Load the design file
            super(application, self).__init__()
            # QtWidgets.QMainWindow.__init__(self, parent)
            self.ui = guiTest.Ui_MainWindow()
            self.ui.setupUi(self)
            
            self.ui.comboBox.activated.connect(self.on_comboBox_activated1)
            
        def on_comboBox_activated1(self, value):
            print('== Enter in on_comboBox_activated ==')
            print('value = {0}'.format(value))
            print('== Leave on_comboBox_activated ==')
    

    What did I do wrong ?

    Sincerely,

    jsulmJ 1 Reply Last reply
    0
    • T teddy.ispsystem

      Hello every one,

      I am working on python (3.8.1) gui using PyQt5 (5.12.3). I am working on windows 10. To build my gui I used QtDesigner (on QtCreator 4.13.3) and I convert it using pyuic5. If you want to reproduce the issue I placed all the file here: https://www.transfernow.net/dl/20210429OllW9k9q

      Files :

      • cmdExport.bat : command used to convert the .ui file
      • test.ui : File created with QtCreator using QtDesigner
      • application.py : File containing my program
      • test.py : File generated by the cmdExport.bat

      So here is my problem :

      I create a class which instantiate the gui generated by pyuic5

      class application(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
              # Load the design file
              super(application, self).__init__()
              # QtWidgets.QMainWindow.__init__(self, parent)
              self.ui = guiTest.Ui_MainWindow()
              self.ui.setupUi(self)
              
              self.ui.comboBox.activated.connect(self.on_comboBox_activated)
              
          def on_comboBox_activated(self, value):
              print('== Enter in on_comboBox_activated ==')
              print('value = {0}'.format(value))
              print('== Leave on_comboBox_activated ==')
      

      The problem is when I clicked on the combo box the slot on_comboBox_activated is called 3 times. If I comment the line :

      self.ui.comboBox.activated.connect(self.on_comboBox_activated)
      

      The slot is also called but 2 times. It seems that if I call a function on_comboBox_activated on the class which instantiate the gui it will be called somewhere. To "solve" the problem I have to change the name of the function in my class like that :

      class application(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
              # Load the design file
              super(application, self).__init__()
              # QtWidgets.QMainWindow.__init__(self, parent)
              self.ui = guiTest.Ui_MainWindow()
              self.ui.setupUi(self)
              
              self.ui.comboBox.activated.connect(self.on_comboBox_activated1)
              
          def on_comboBox_activated1(self, value):
              print('== Enter in on_comboBox_activated ==')
              print('value = {0}'.format(value))
              print('== Leave on_comboBox_activated ==')
      

      What did I do wrong ?

      Sincerely,

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @teddy-ispsystem said in Combo Box calling slot without connect:

      self.on_comboBox_activated

      With this slot name you're using the auto-connect feature. Rename the slot (remove on_ prefix for example). Also: did you connect in QtDesigner already? All that would explain why it is called 3 times.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • T Offline
        T Offline
        teddy.ispsystem
        wrote on last edited by
        #3

        Oh ! I didn't know about auto-connect feature it explain everything. It's called 3 times because the slot "activated" has 2 overloading (using QString or int). If I want to filter it I have to use pyqtSlot module and add :

        @pyqtSlot(str)
        

        or

        @pyqtSlot(int)
        

        Before the slot.

        Many thanks !

        1 Reply Last reply
        1

        • Login

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