开发者

Why does SLOT('insertColumn()') fail in PyQt binding?

开发者 https://www.devze.com 2023-02-07 06:25 出处:网络
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 o

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.setWin开发者_C百科dowTitle('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 call self.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.


While associating the signal with a slot you need to provide only the method signture and you shouldn't be passing the arguments to it. Also the signature of the method you provide as a slot should match the signature of the signal. As per the Qt docs the signature of the triggered signal of QAction is as below: (Qt Version 4.7.0)

void QAction::triggered ( bool checked = false )

Essentialy you need to connect the triggered signal to a method that expects a boolean argument.

You can try having a code snippet like below : (untested)

self.connect(self.MenuAddColumn, SIGNAL('triggered(bool)'),
self, SLOT('colAddition(bool)')

def colAddition(self,value):  
    if value:  
        self.tableWidget.insertColumn(2)  

0

精彩评论

暂无评论...
验证码 换一张
取 消