Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with QSqlTableModel -- no automatic updates

Tags:

sql

database

qt

qt4

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
like image 806
MadH Avatar asked Jun 24 '09 15:06

MadH


1 Answers

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?

like image 198
Alex Martelli Avatar answered Nov 07 '22 14:11

Alex Martelli