tags:

views:

901

answers:

3

After setting up a table model in Qt 4.4 like this:

    QSqlTableModel *sqlmodel = new QSqlTableModel();

    sqlmodel->setTable("Names");
    sqlmodel->setEditStrategy(QSqlTableModel::OnFieldChange);
    sqlmodel->select();
    sqlmodel->removeColumn(0);

    tableView->setModel(sqlmodel);
    tableView->show();

the content is displayed properly, but editing is not possible, error:

     QSqlQuery::value: not positioned on a valid record
A: 

It seems that the cause of this was in line

sqlmodel->removeColumn(0);

After commenting it out, everything work perfectly. Thus, I'll have to find another way not to show ID's in the table ;-)

EDIT I've said "it seems", because in the example from "Foundations of Qt development" Johan Thelin also removed the first column. So, it would be nice if someone else also tries this and reports results.

MadH
+4  A: 

I can confirm that the bug exists exactly as you report it, in Qt 4.5.1, AND that the documentation, e.g. here, still gives a wrong example (i.e. one including the removeColumn call).

As a work-around I've tried to write a slot connected to the beforeUpdate signal, with the idea of checking what's wrong with the QSqlRecord that's about to be updated in the DB and possibly fixing it, but I can't get that to work -- any calls to methods of that record parameter are crashing my toy-app with a BusError.

So I've given up on that idea and switched to what's no doubt the right way to do it (visibility should be determined by the view, not by the model, right?-): lose the removeColumn and in lieu of it call tableView->setColumnHidden(0, true) instead. This way the IDs are hidden and everything works.

So I think we can confirm there's a documentation error and open an issue about it in the Qt tracker, so it can be fixed in the next round of docs, right?

Alex Martelli
yes, thanks! :-)
MadH
spent all of my reputation to confirm that :-))
MadH
A: 

I use Qt 4.6.1 in PyQt and the problem is still here. Removing "removeColumn(0)" solves the issue.

Artiom