PDA

View Full Version : TableView + SqlQueryModel: can't insert or edit rows



jiveaxe
24th September 2008, 17:13
Hi,
I have a problem editing or inserting rows in a QTableView + QSqlQueryModel.

Here my code:


MainDialog::MainDialog(QWidget *parent)
:QDialog(parent)
{
...
scoopsModel = new QSqlQueryModel;
scoopsModel->setQuery("SELECT numero,pagina,sottotitolo FROM elenco_scoop WHERE id_gioco="
+ articlesComboBox->itemData(articlesComboBox->currentIndex()).toString()
+ " ORDER BY numero");
scoopsModel->setHeaderData(0, Qt::Horizontal, tr("Numero"));
scoopsModel->setHeaderData(1, Qt::Horizontal, tr("Pagina"));
scoopsModel->setHeaderData(2, Qt::Horizontal, tr("Sottotitolo"));
scoopsTableView->setModel(scoopsModel);
scoopsTableView->show();

connect(scoopAddButton, SIGNAL(clicked()), this, SLOT(addScoop()));
...
}



void MainDialog::addScoop()
{
((QSqlQueryModel*)scoopsTableView->model())->insertRows(((QSqlQueryModel*)scoopsTableView->model())->rowCount(), 1);
}

1) First problem: if I double click a cell of a row it doesn't switch in edit mode;
2) Second problem: when clicking scoopAddButton no new row is inserted; insertRows(..) returns false.

Where is the error?

Thanks

janus
24th September 2008, 18:02
Hi,

I guess you need a QSqlTableModel. QueryModel is read only, I suppose.

jiveaxe
24th September 2008, 18:31
Why list this function among QSqlQueryModel's members if it doesn't work?

Regards

jacek
27th September 2008, 22:55
Why list this function among QSqlQueryModel's members if it doesn't work?
QSqlQueryModel docs don't list it. This method exists, because it was inherited from QAbstractItemModel.