[PyQt] QAbstractItemModel's "dataChanged" signal not working?

Claudio Felix felix.claudio at gmail.com
Sat Feb 27 21:55:15 GMT 2010

Hi everyone,

I'm using a QSqlRelationalTableModel for a simple dialog where I can
add/delete periods related to a particular customer, which is chosen
by a QComboBox. The periods table is filtered by customer (whose ID is
a foreign key) and shown through a QTableView. There's an "Add" button
which basically inserts a new row in the Periods table and sets the
view to edit mode, so the period data can be entered:

    def addRecord(self):
        row = self.model.rowCount()
        customerid = self._getRecordID(self.customerComboBox,
self.customersModel, "CUSTOMER_ID")
        index = self.model.index(row, CUSTOMER_ID)
        self.model.setData(index, QVariant(customerid))
        index = self.model.index(row, PERIOD_YEAR)

I noticed that, while the view is in edit mode (with the asterisk '*'
appearing in the leftmost field), if the user clicks on the add button
again, an empty row appears in the view and the asterisk appears in
the next row, with this message on the console:

edit: index was invalid
edit: editing failed

The same problem happens in the "assetmanager.pyw" example from the
(great!) book "Rapid GUI Programming with Python and QT", which is my
main guide. That way, in my limited experience with PyQT, I tried to
work around the undesirable behavior creating the method
"enableControls" for the dialog, which is called in the addRecord
method so it makes it impossible for the user to click on "add" again
while the view is in edit mode. The idea then was to re-enable the
controls when the data was finally committed by the view. That's when
the main problem comes up. I tried to use the model's "dataChanged"
signal for calling my "enableControls" method, but it looks like it
never gets emitted, although the record does get written to the
database table. Does anybody can confirm that or help me avoiding the
problem at all? I used the following signature for the signal, exactly
the same shown on QAbstractItemModel's documentation:

self.connect(self.model, SIGNAL("dataChanged(const QModelIndex&,const
QModelIndex&)"), self.enableControls)

That line is declared in my dialog's __init__, along with all the
other (working) signals. self.model referes to the

Thanks for any help!


More information about the PyQt mailing list