PDA

View Full Version : Insert row work only ones



thefatladysingsopera
8th September 2011, 08:34
I have a QTableView and i am using this code to insert new data.


void smith::on_newButton_clicked()
{

ui.fEdit->clear();
ui.lnEdit->clear();
ui.cEdit->clear();
ui.cityEdit->clear();

ui.newButton->setEnabled(false);
ui.saveEdits->show();
ui.saveNew->show();
ui.cancelButton->show();
ui.nextButton->setEnabled(false);
ui.previousButton->setEnabled(false);
ui.deleteButton->setEnabled(false);
}

The function clears the forms and takes in new data and upon a click on the save button,the input is passed to saveNew function:


void smith::saveNew()
{
//insert code above...
query.bindValue(":firstname", ui.fEdit->text());
query.bindValue(":lastname", ui.lnEdit->text());
query.bindValue(":country", ui.cEdit->text());
query.bindValue(":city", ui.cityEdit->text());
int das = query.lastInsertId().toInt();
query.exec();

tableModel->submitAll();
int row = tableModel->rowCount();
tableModel->insertRow(row);
QModelIndex index = tableModel->index(das, firstname);
ui.tableView->setCurrentIndex(index);
ui.tableView->edit(index);

ui.newButton->setEnabled(true);
ui.saveNew->setEnabled(true);
ui.saveEdits->setEnabled(true);
ui.nextButton->setEnabled(true);
ui.previousButton->setEnabled(true);
ui.cancelButton->setEnabled(true);
ui.deleteButton->setEnabled(true);


}
When i click i enter data and click input it works as expected(a new row is inserted,data is inserted to sqlite and the newly created data is presented for editing.However,when i click new button again and click save,no new row is inserted and the lastinsertedrecord shows what i inserted the immediate previous time and not the most new insert but still data is still inserted to sqlite.

What could be the problem?.

wysota
8th September 2011, 11:19
It is likely you should use "row" instead of "das" when computing the index to modify the data with. Also if you submit the table, I think you should select() it back again.

thefatladysingsopera
8th September 2011, 12:23
I am using "das" to hold the ID of the last inserted record,and surprisingly it works,but only once.I am looking at a past thread of yours http://www.qtcentre.org/threads/29532-QAbstractItemModel-reset-Persistent-Index to see if i can learn something from it.

wysota
8th September 2011, 12:45
I am using "das" to hold the ID of the last inserted record,and surprisingly it works
It will stop working the first time you decide to delete a record from the table.

thefatladysingsopera
8th September 2011, 13:56
It will stop working the first time you decide to delete a record from the table.

I have not finished writing the delete function but i think it will work because the index work with this time will be:


QModelIndex index = ui.tableView->currentIndex();

but i will try.

wysota
8th September 2011, 14:41
It will not work because if you have 10 rows with id 1-10 and you delete row with id say... "7" then you end up with 9 rows but next time you call lastInsertId() you will get "11" however you will have only 10 rows in the database and your index() call will return an invalid model index.


QModelIndex index = tableModel->index(das, firstname); // tableModel->rowCount()==10, das == 11 => index(11, ...) == QModelIndex()

Besides, if you are using QSqlTableModel then insert rows through that table model and not with an external query, your model doesn't know that you inserted a new row through an external query until you call select() on it again. You have a "nice" synchronization problem here.

thefatladysingsopera
8th September 2011, 17:11
I got rid of "das" and now have:-


tableModel->select();
..

int row = tableModel->rowCount();
tableModel->insertRow(row);
QModelIndex index = tableModel->index(row, firstname);
ui.tableView->setCurrentIndex(index);
ui.tableView->edit(index);
tableModel->submitAll();
When i run the application, and click save to insert a new row,a blank row is inserted with an asterick(also discussed here: http://developer.qt.nokia.com/forums/viewthread/9064)

I have run out of ideas on how to move forward.

wysota
8th September 2011, 20:05
What code are you calling from the save button?

thefatladysingsopera
9th September 2011, 13:01
It finally worked after select() again.


int row = tableModel->rowCount();
tableModel->insertRow(row);
tableModel->select();
QModelIndex index = tableModel->index(row, firstname);
ui.tableView->setCurrentIndex(index);
ui.tableView->edit(index);
tableModel->submitAll();