[PyQt] TypeError thrown by rowsAboutToBeRemoved() when deleting a row from a QAbstractTableModel

Simon Hibbs simon.hibbs at gmail.com
Wed Jul 14 08:25:30 BST 2010


The method implementation looks like this:

def removeRows(self, position, rows=1, index=QModelIndex()):
    # problem on next line, which raises rowsAboutToBeRemoved signal
    self.beginRemoveRows(QModelIndex(), position, position + rows - 1)
    self.units = self.units[:position] + \
                 self.units[position + rows:]
    self.endRemoveRows()
    self.dirty = True
    return True

The error is:

TypeError: rowsAboutToBeRemoved() takes exactly 3 arguments (4 given)

The code is identical to the example in the Rapid GUI Programming
book. In fact the example code from the book I downloaded works and
mine doesn't on the same box, but I can't for the life of me see why.
I have also upgraded from PyQt 4.6.1 to 4.7.4 and still have the
problem.

I think I can guess what is happening. Python adds an implicit self
reference as the first argument to any method call, hence 4 arguments
not three, but rowsAboutToBeRemoved lives in C++ land so doesn't know
what to do with the self reference. However how come the example code
works and mine doesn't? I know you can't answer that, but has anyone
seen anything like this before? Any debugging tricks I can use to get
more information on the problem? If I print the beginRemoveRows
arguments there are only 3 of them, a QModelIndex object reference and
two integers.

I suppose if the beginRemoveRows invocation is correct, and I'm sure
it is, it must be something to do with argument passing between PyQt
and C++. There's a note in the Riverbank docs for QAbstractItemModel
constructor that says:

"The parent argument, if not None, causes self to be owned by Qt
instead of PyQt."

I tried setting parent=None but the book example code doesn't do that
and it didn't make any difference anyway. Is there anything I can do,
any instrumentation I can put into the app that might give more info
on the issue?

Best regards,

Simon Hibbs


More information about the PyQt mailing list