Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Combo Box calling slot without connect



  • 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,


  • Lifetime Qt Champion

    @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.



  • 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 !


Log in to reply