Reputation: 51
I am having trouble understanding how i access the returned elements when i implement a QSqlQueryModel.
I know that you can do QSqlQuery query;
query.prepare("select * from database");
query.exec();
query.next();
qDebug() << "value in 0 is " << query.value(0).SomeFormat;
So i want to do something like that with QSqlQueryModel (apparently the better way to go).. where i set the query, then i can output the values to another lot of boxes i have. what i have so far is...
QSqlQuery selectAllUserFields;
selectAllUserFields.prepare(QString("SELECT * from %1 WHERE %2=:firstName and %3=:lastName;")
.arg(dbase::c_userTableName)
.arg(dbase::c_colUserFirstName)
.arg(dbase::c_colUserSecondName));
// finds the index of the current selection, so we can select the row
QModelIndexList tableIndex = m_ui->populatedUserBox->selectionModel()->selection().indexes();
QString firstName = tableIndex.at(0).data().toString();
QString lastName = tableIndex.at(1).data().toString();
QSqlQueryModel dbUsers;
dbUsers.setQuery(selectAllUserFields);
qDebug() << "DEBUG: {temp} " << dbUsers.record(0).value(0).toString();
I am beginnerish, so would appreciate a nudge in the right direction if anyone could assist.
Thanks Grant
Upvotes: 4
Views: 8341
Reputation: 261
I suggest you have a look at the QSqlQueryModel documentation "Detailed Description" section where it gives an example. To help you further, here is some code that I wrote for an application to iterate over the model result set:
for(int i = 0; i < sqlQueryModel->rowCount(); ++i)
{
qDebug() << sqlQueryModel->record(i).value(0).toString();
}
Another way to do it is to use the function QSqlQueryModel::data(); Again I suggest that you review the documentation here: http://doc-snapshot.qt-project.org/4.8/qsqlquerymodel.html
Upvotes: 3