TableWidget.insertRow(1) fails inside connect() call



  • Newbie question: I used pyuic5 to get a .py file of a simple test-case gui from Designer:

    -two widgets, one dialog

    • widget 1: table with 2 rows and 2 columns
    • widget 2: push button, intended to add a row to the table on each push

    In Designer, the insertRow mehtod does not exist as an option, so I saved the .ui and made the .py from it using the selectAll slot instead; the corresponding line in the generated .py is:

        self.pushButton.clicked.connect(self.tableWidget.selectAll)
    

    And that works just fine.

    Changing that line to
    self.pushButton.clicked.connect(self.tableWidget.insertRow)
    also works and adds a row at the top; but, I'd like to add a row at the bottom, something line .insertRow(self.tableWidget.rowCount) but as a first test I just tried .insertRow(1):

        self.pushButton.clicked.connect(self.tableWidget.insertRow(1))
    

    and this returns an error:

    c:\Python34\Lib\site-packages\PyQt5>python dialog.py
    Traceback (most recent call last):
    File "dialog.py", line 8, in <module>
    uio.setupUi(window)
    File "c:\Python34\Lib\site-packages\PyQt5\ui.py", line 34, in setupUi
    self.pushButton.clicked.connect(self.tableWidget.insertRow(1))
    TypeError: argument 1 has unexpected type 'NoneType'

    Any idea why that's happening? No doubt this is a newbie mistake, but, any info to help resolve it would be much appreciated. Thanks



  • Solved it with the help of "this":http://qt-project.org/forums/viewthread/17393 post.

    Changing to
    self.pushButton.clicked.connect(lambda:self.tableWidget.insertRow(1))

    (basically just prefixing it with 'lambda:')

    solves it; I didn't realize connect() expects a reference to a function. Yup, a newbie problem indeed.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    It's rather that you are giving the slot a parameter. You also can't connect a parameterless signal to a slot with parameter.

    Anyway, for your purpose the use of a lambda is perfectly fine


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.