4

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

1
  • Maybe you can iterate through model's records from 0 to QSqlQueryModel::rowCount? Commented Jul 26, 2012 at 6:25

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.