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

Impossible to connect slots in TabWidget



  • Hi, I'm trying to develop a simple application with a MainWindows containing a TabWidget made with three pages. In one of them i setted up some buttons but I'm not able to trigger the slot functions.

    // main.py
    from PyQt5.QtGui import QIcon
    from PyQt5.QtWidgets import (QApplication, QWidget, QMainWindow, QTabWidget)
    from Views.PagesImplementation import ClassifyPageUIClass, MainWindowUIClass
    import sys
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
    
        ClassifyPage = QWidget()
        ui = ClassifyPageUIClass()
        ui.setupUi(ClassifyPage)
    
        tabsSubPage = QTabWidget()
        tabsSubPage.addTab(ClassifyPage, QIcon("zoom.png"), "Classify")
    
        MainWindow = QMainWindow()
        ui = MainWindowUIClass()
        ui.setupUi(MainWindow)
        MainWindow.setCentralWidget(tabsSubPage)
        MainWindow.show()
    
        sys.exit(app.exec_())
    
    //ClassifyPage (generated from QtDesigner)
    from PyQt5 import QtCore, QtWidgets
    from PyQt5.QtCore import QObject, pyqtSlot
    
    
    class Ui_ClassifyPage(QObject):
        def setupUi(self, ClassifyPage):
            ClassifyPage.setObjectName("ClassifyPage")
            ClassifyPage.resize(500, 480)
            self.widget = QtWidgets.QWidget(ClassifyPage)
            self.widget.setGeometry(QtCore.QRect(40, 21, 436, 391))
            self.widget.setObjectName("widget")
            self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
            self.verticalLayout.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout.setObjectName("verticalLayout")
            self.getFileGroupBox = QtWidgets.QGroupBox(self.widget)
            self.getFileGroupBox.setObjectName("getFileGroupBox")
            self.gridLayout = QtWidgets.QGridLayout(self.getFileGroupBox)
            self.gridLayout.setObjectName("gridLayout")
            self.horizontalLayout = QtWidgets.QHBoxLayout()
            self.horizontalLayout.setObjectName("horizontalLayout")
            self.listView = QtWidgets.QListView(self.getFileGroupBox)
            self.listView.setObjectName("listView")
            self.horizontalLayout.addWidget(self.listView)
            self.browsePushbutton = QtWidgets.QPushButton(self.getFileGroupBox)
            self.browsePushbutton.setEnabled(True)
            self.browsePushbutton.setObjectName("browsePushbutton")
            self.horizontalLayout.addWidget(self.browsePushbutton, 0, QtCore.Qt.AlignRight|QtCore.Qt.AlignTop)
            self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
            self.verticalLayout.addWidget(self.getFileGroupBox)
            spacerItem = QtWidgets.QSpacerItem(448, 118, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
            self.verticalLayout.addItem(spacerItem)
            self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
            self.horizontalLayout_2.setSpacing(10)
            self.horizontalLayout_2.setObjectName("horizontalLayout_2")
            self.cancelPushButton = QtWidgets.QPushButton(self.widget)
            self.cancelPushButton.setObjectName("cancelPushButton")
            self.horizontalLayout_2.addWidget(self.cancelPushButton)
            spacerItem1 = QtWidgets.QSpacerItem(193, 27, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
            self.horizontalLayout_2.addItem(spacerItem1)
            self.classifyPushButton = QtWidgets.QPushButton(self.widget)
            self.classifyPushButton.setObjectName("classifyPushButton")
            self.horizontalLayout_2.addWidget(self.classifyPushButton)
            self.verticalLayout.addLayout(self.horizontalLayout_2)
    
            self.retranslateUi(ClassifyPage)
            self.browsePushbutton.clicked.connect(self.pickDatasetSlot)
            self.classifyPushButton.clicked.connect(self.classifySlot)
            self.cancelPushButton.clicked.connect(self.cancelSlot)
            QtCore.QMetaObject.connectSlotsByName(ClassifyPage)
    
        def retranslateUi(self, ClassifyPage):
            _translate = QtCore.QCoreApplication.translate
            ClassifyPage.setWindowTitle(_translate("ClassifyPage", "Form"))
            self.getFileGroupBox.setTitle(_translate("ClassifyPage", "Select a file or folder to classify"))
            self.browsePushbutton.setText(_translate("ClassifyPage", "Browse"))
            self.cancelPushButton.setText(_translate("ClassifyPage", "Cancel"))
            self.classifyPushButton.setText(_translate("ClassifyPage", "Classify"))
    
        @pyqtSlot( )
        def pickDatasetSlot(self):
            pass
    
        @pyqtSlot( )
        def classifySlot(self):
            pass
    
        @pyqtSlot( )
        def cancelSlot(self):
            pass
    
    // ClassifyPageClass
    class ClassifyPageUIClass(Ui_ClassifyPage):
        def __init__(self):
            '''Initialize the super class
            '''
            super().__init__()
    
        def setupUi(self, MW):
            ''' Setup the UI of the super class, and add here code
            that relates to the way we want our UI to operate.
            '''
            super().setupUi(MW)
    
        def pickDatasetSlot(self):
            print("dataset")
    
        def classifySlot(self):
            print("classify")
    
        def cancelSlot(self):
            print("cancel")
    

    By running just the ClassifyPageClass instance the slots works simply well. What am I doing wrong?



  • I dont think you need to use @pyqtSlot decorator.
    At least I never do.



  • I found out the error and I feel so stupid. I used the same name (ui) for two variables in

        ClassifyPage = QWidget()
        ui = ClassifyPageUIClass()
        ui.setupUi(ClassifyPage)
    

    and

        MainWindow = QMainWindow()
        ui = MainWindowUIClass()
        ui.setupUi(MainWindow)
    

    so the the slots connects of my QTabWidget were overwrighted.


Log in to reply