PDA

View Full Version : Selecting a row in the a tableview after inserting it.



naptizaN
17th July 2012, 12:39
Hello, I would like to select a row in the a tableview after inserting it. I'm inserting the row using QSqlQuery. I want to select the inserted row in a QTableView which uses this model. I can see the added row in tableview but my problem is I cannot find QModelIndex which corresponds to the new row in the model. And i use QSortFilterProxyModel between the model and the view.
I know that i must use:

QModelIndex i;
view->selectionModel()->select(i, QItemSelectionModel::Select);
How can i find QModelIndex of the inserted row and select it in tableview?

aamer4yu
17th July 2012, 13:09
How are you inserting the row in first place ??

naptizaN
17th July 2012, 13:21
using such code:

void MainWindow::insertRow()
{
QSqlDatabase db=QSqlDatabase::database();
QString sqlstr=QString("INSERT INTO sianimtable (CIPHER,CIPHERKP,TITLE,MSGON,MSGOFF) VALUES(\"%1\",\"%2\",\"%3\",\"%4\",\"%5\");")
.arg(le2->text())
.arg(le1->text())
.arg(le3->text())
.arg(le4->text())
.arg(le5->text());


QSqlQuery q(db);
if(q.exec(sqlstr)==false)
{
QMessageBox *pmsg = new QMessageBox;
pmsg->setText("Не могу соединиться с базой данных");
pmsg->setInformativeText(q.lastError().text());
pmsg->exec();
return;
}
modelt->select();
}

naptizaN
18th July 2012, 14:15
a bit changed code, but still need to select somehow.

void MainWindow::insertRow()
{
QSqlQuery *q = new QSqlQuery();
q->prepare("INSERT INTO sianimtable (CIPHER,CIPHERKP,TITLE,MSGON,MSGOFF) VALUES(:le1,:le2,:le3,:le4,:le5);");
q->bindValue(":le2",le1->text());
q->bindValue(":le1",le2->text());
q->bindValue(":le3",le3->text());
q->bindValue(":le4",le4->text());
q->bindValue(":le5",le5->text());

if(q->exec()==false)
{
QMessageBox *pmsg = new QMessageBox;
pmsg->setText("Не могу соединиться с базой данных");
pmsg->setInformativeText(q->lastError().text());
pmsg->exec();
}
delete q;
}