PDA

View Full Version : rowsInserted and rowsRemoved signals with QSqlTableModel



Nero
19th July 2010, 08:48
Hello,
I have a QSqlTableModel and its signals rowsInserted and rowsRemoved connected to print their parameters:



model = new QSqlTableModel;
model->setTable("tab");
model->select();
connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
this, SLOT(sourceRowsInserted(QModelIndex,int,int)));
connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(sourceRowsRemoved(QModelIndex,int,int)));
...

void ETest::sourceRowsInserted(const QModelIndex &source_parent, int start, int end)
{
qDebug() << "rowsInserted" << start << end;
}

void ETest::sourceRowsRemoved(const QModelIndex &source_parent, int start, int end)
{
qDebug() << "rowsRemoved" << start << end;
}

void ETest::addme()
{
QSqlRecord record = model->record();
model->insertRecord(-1, record);
}

void ETest::deleteme()
{
model->removeRow(model->rowCount() - 1);
}

When I use OnManualSubmit as strategy it is perfect:
output inserting the fourth row:
rowsInserted 3 3
output removing the fourth row:
rowsRemoved 3 3

But when I use OnFieldChange or OnManualSubmit:
output inserting the fourth row:
rowsInserted 3 3
rowsRemoved 0 3
rowsInserted 0 3
output removing the fourth row:
rowsRemoved 0 3
rowsInserted 0 2

Stepping into Qt, I've found a call to submit and select:


bool QSqlTableModel::insertRecord(int row, const QSqlRecord &record)
{
...
if (d->strategy == OnFieldChange || d->strategy == OnRowChange)
return submit();

bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent)
{
...
switch (d->strategy) {
case OnFieldChange:
case OnRowChange:
...
select();

I suppose this is a wanted behavior but it makes rowsInserted and rowsRemoved signals very hard to use.
I need to make a simple ModelProxy to populate a tree view with a QSqlTableModel, how can I get a clean signal when a row is inserted or removed?