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. Is there a way to avoid pyuic5 generating connectSlotsByName() automatically?
Forum Update on Monday, May 27th 2025

Is there a way to avoid pyuic5 generating connectSlotsByName() automatically?

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 3 Posters 955 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.
  • Q Offline
    Q Offline
    Qinhan Hou
    wrote on last edited by
    #1

    I'm using Qt Designer to design UI of my application, and using pyuic5 to transfer UI files to python code. Code generated by pyuic5 would be like:

    # mainwindow.py
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(600, 400)
            self.tableWidget = QtWidgets.QTableWidget(MainWindow)
            self.tableWidget.setObjectName("tableWidget")
            self.tableWidget.setColumnCount(10)
            self.tableWidget.setRowCount(10)
            # hide QTableWidgetItem generation code here
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            # hide some code here
    

    In general, I need to create a new class which is derived from the class above to realize the response of every widget. My code would be like:

    # mymainwindow.py
    from PyQt5 import QtCore, QtWidgets
    from mainwindow import Ui_MainWindow
    
    class MyMainWindow(QtWidgets.QDialog, Ui_MainWindow):
        def __init__(self, parent=None):
            super(MyMainWindow, self).__init__(parent)
            self.data = read_list_from_external_file()
            self.setupUi(self)
    
        def setupUi(self, my_mainwindow):
            super(MyDialog, self).setupUi(my_mainwindow)
            for row in range(self.tableWidget.rowCount()):
                for col in range(self.tableWidget.columnCount()):
                    self.tableWidget.setItem(row, col, QtWidgets.QTableWidgetItem(self.data[row][col]))
    
        @QtCore.pyqtSlot(int, int)
        def on_tableWidget_cellChanged(self, row, col):
            # some response on cell changed
    

    In my code, the slot method on_tableWidget_cellChanged() will be executed when I fill in tableWidget, but I don't want to do that in setup stage. The key point is that connectSlotsByName() needs to be executed after setup stage. Of course I can use tableWidget.blockSignals(), or give up using decorator and connect manually. But actually there are dozens of widgets in my application, neither solution looks like the "best" one in my mind. I expect a way to avoid generating connectSlotsByName() automatically, then I can add it in the end of setupUi() of MyMainWindow manually.
    Then I found in Qt Designer, there is a option to control whether connectSlotsByName() is added in the code.
    671dc7b4-cebf-4b79-acce-c54103e59d44-image.png
    If I uncheck this option, the header of .ui file will be modified from

    <ui version="4.0">
    

    to

    <ui version="4.0" connectslotsbyname="false">
    

    But it seems like pyuic5 doesn't recognize this change. connectSlotsByName() is still there. Is there a way to avoid pyuic5 generating connectSlotsByName() automatically? Or is there an "elegant" way to avoid slot methods executing in setup stage?

    JonBJ 1 Reply Last reply
    0
    • Q Qinhan Hou

      I'm using Qt Designer to design UI of my application, and using pyuic5 to transfer UI files to python code. Code generated by pyuic5 would be like:

      # mainwindow.py
      from PyQt5 import QtCore, QtGui, QtWidgets
      
      class Ui_MainWindow(object):
          def setupUi(self, MainWindow):
              MainWindow.setObjectName("MainWindow")
              MainWindow.resize(600, 400)
              self.tableWidget = QtWidgets.QTableWidget(MainWindow)
              self.tableWidget.setObjectName("tableWidget")
              self.tableWidget.setColumnCount(10)
              self.tableWidget.setRowCount(10)
              # hide QTableWidgetItem generation code here
              self.retranslateUi(MainWindow)
              QtCore.QMetaObject.connectSlotsByName(MainWindow)
      
          def retranslateUi(self, MainWindow):
              # hide some code here
      

      In general, I need to create a new class which is derived from the class above to realize the response of every widget. My code would be like:

      # mymainwindow.py
      from PyQt5 import QtCore, QtWidgets
      from mainwindow import Ui_MainWindow
      
      class MyMainWindow(QtWidgets.QDialog, Ui_MainWindow):
          def __init__(self, parent=None):
              super(MyMainWindow, self).__init__(parent)
              self.data = read_list_from_external_file()
              self.setupUi(self)
      
          def setupUi(self, my_mainwindow):
              super(MyDialog, self).setupUi(my_mainwindow)
              for row in range(self.tableWidget.rowCount()):
                  for col in range(self.tableWidget.columnCount()):
                      self.tableWidget.setItem(row, col, QtWidgets.QTableWidgetItem(self.data[row][col]))
      
          @QtCore.pyqtSlot(int, int)
          def on_tableWidget_cellChanged(self, row, col):
              # some response on cell changed
      

      In my code, the slot method on_tableWidget_cellChanged() will be executed when I fill in tableWidget, but I don't want to do that in setup stage. The key point is that connectSlotsByName() needs to be executed after setup stage. Of course I can use tableWidget.blockSignals(), or give up using decorator and connect manually. But actually there are dozens of widgets in my application, neither solution looks like the "best" one in my mind. I expect a way to avoid generating connectSlotsByName() automatically, then I can add it in the end of setupUi() of MyMainWindow manually.
      Then I found in Qt Designer, there is a option to control whether connectSlotsByName() is added in the code.
      671dc7b4-cebf-4b79-acce-c54103e59d44-image.png
      If I uncheck this option, the header of .ui file will be modified from

      <ui version="4.0">
      

      to

      <ui version="4.0" connectslotsbyname="false">
      

      But it seems like pyuic5 doesn't recognize this change. connectSlotsByName() is still there. Is there a way to avoid pyuic5 generating connectSlotsByName() automatically? Or is there an "elegant" way to avoid slot methods executing in setup stage?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Qinhan-Hou
      If you say unchecking that option does not seem to work from PyQt5 (I don't know whether it does or not, I'm surprised it does not but if that is what you say), then the best i can suggest is: do not name your slots in the connectSlotsByName() way, pick anything else. Then it won't find the slots to connect (which is fine) and you can do your own manual connections when you choose to.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        You can also use the blockSignals method however having explicit connections will be simpler in the long run.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          Qinhan Hou
          wrote on last edited by
          #4

          @JonB @SGaist
          Thanks for your suggestions!
          I have thought of the solution you mentioned. Maybe it's the best way so far (before this pyuic5 bug fixed).

          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