[Qt-interest] QSqlTableModel doesn't save new entries, editing works fine

Sven Grunewaldt strayer at olle-orks.org
Sun Feb 1 22:26:46 CET 2009


Hi,

I use a QSqlTableModel to display a simple SQLite database. When I edit 
an entry everything works correctly, but as soon as I want to add an 
entry it doesn't "submit" the changes.
The row is added to the tableView and can be edited, but the 
beforeInsert Slot won't be executed and there is no change to the database.

I'd appreciate any help, I'm at a total loss...

Here is the part of the code I use:

BerichteOrk::BerichteOrk(QWidget *parent) : QMainWindow(parent)
{
     [...]

     schuleModel = new QSqlTableModel(this);
     schuleModel->setTable("entry");
     schuleModel->setFilter("1 = 0"); // Will be changed by Signals/Slots
     schuleModel->setHeaderData(1, Qt::Horizontal, tr("Fach"));
     schuleModel->setHeaderData(2, Qt::Horizontal, tr("Thema"));
     schuleModel->select();

     schuleView->setModel(schuleModel);
     schuleView->setColumnHidden(0, true);
     schuleView->setColumnHidden(3, true);
     schuleView->setColumnHidden(4, true);
     schuleView->setColumnHidden(5, true);

     [...]
}

void BerichteOrk::on_addSchuleButton_clicked()
{
     int row = schuleModel->rowCount();
     schuleModel->insertRow(row);

     QModelIndex index = schuleModel->index(row, 1);
     schuleView->setCurrentIndex(index);
     schuleView->edit(index);
}

void BerichteOrk::on_schuleModel_beforeInsert(QSqlRecord &record)
{
     record.setValue("year", selectedYear);
     record.setValue("week", selectedWeek);
     record.setValue("type", "school");
}



More information about the Qt-interest-old mailing list