Is there a way to avoid pyuic5 generating connectSlotsByName() automatically?
-
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 intableWidget
, but I don't want to do that in setup stage. The key point is thatconnectSlotsByName()
needs to be executed after setup stage. Of course I can usetableWidget.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 generatingconnectSlotsByName()
automatically, then I can add it in the end ofsetupUi()
ofMyMainWindow
manually.
Then I found in Qt Designer, there is a option to control whetherconnectSlotsByName()
is added in the code.
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 generatingconnectSlotsByName()
automatically? Or is there an "elegant" way to avoid slot methods executing in setup stage? -
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 intableWidget
, but I don't want to do that in setup stage. The key point is thatconnectSlotsByName()
needs to be executed after setup stage. Of course I can usetableWidget.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 generatingconnectSlotsByName()
automatically, then I can add it in the end ofsetupUi()
ofMyMainWindow
manually.
Then I found in Qt Designer, there is a option to control whetherconnectSlotsByName()
is added in the code.
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 generatingconnectSlotsByName()
automatically? Or is there an "elegant" way to avoid slot methods executing in setup stage?@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 theconnectSlotsByName()
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. -
Hi,
You can also use the blockSignals method however having explicit connections will be simpler in the long run.