I’m diving into PyQt, and I’ve come across a little annoyance. SLOT(‘insertColumn()’) does nothing when attached to a button in the GUI. SLOT(‘clear()’) works perfectly, and insertColumn() works outside of the binding. insertColumn() is listed as a public slot in the Qt documentation, so I’m stuck.
Here’s the code:
class MainWindow(QMainWindow):
def __init__(self, *args):
QMainWindow.__init__(self, *args)
# Table
self.tableWidget = QTableWidget(6, columnCount, self);
x = 0
for x in range(0, columnCount):
self.tableWidget.setColumnWidth(x, 30)
for x in range(0, 6):
self.tableWidget.setRowHeight(x, 24)
# Window geometery and layout
self.resize(800, 600)
self.setWindowTitle('PyTab')
self.setCentralWidget(self.tableWidget)
# Menu and toolbar actions
self.MenuExit = QAction(QIcon('exit.png'), 'Exit', self)
self.MenuExit.setShortcut('Ctrl+Q')
self.connect(self.MenuExit, SIGNAL('triggered()'), SLOT('close()'))
self.MenuAddColumn = QAction(QIcon('clear.png'), 'Add Column', self)
self.MenuAddColumn.setShortcut('Ctrl+N')
#This line works:
self.tableWidget.insertColumn(2)
# I'm not sure why this line isn't working:
self.connect(self.MenuAddColumn, SIGNAL('triggered()'), self.tableWidget, SLOT('insertColumn(2)'))
I’ve tried defining the QTableWidget as tableWidget and self.tableWidget, but that didn’t change anything.
Does anybody know what’s causing this?
Here’s the final code:
def add_column(self):
tableWidget.insertColumn(1)
self.MenuAddColumn.triggered.connect(add_column)
It uses the new .connect() syntax as described in this SO question. Thanks again!
The call to
.connect()only sets up the relationship for the signal and the slot. You can’t actually pass values to the slot in the.connect()call. You can create your own slot and put that in the call to.connect()and then in your own slot implementation callself.tableWidget.insertColumn(2).Edit:
Just to clarify..although the slot can’t directly take values in the call to
.connect(), it can receive parameters that are defined for the signal.