我在实现 QSqlQueryModel 时无法理解如何访问返回的元素。
我知道你可以做到
QSqlQuery 查询;
query.prepare("select * from database");
query.exec();
query.next();
qDebug() << "value in 0 is " << query.value(0).SomeFormat;
所以我想用 QSqlQueryModel 做类似的事情(显然是更好的方法)..在我设置查询的地方,然后我可以将值输出到我拥有的另一批盒子中。
到目前为止我所拥有的是......
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();
我是初学者,所以如果有人能提供帮助,我将不胜感激。
谢谢
授予
最佳答案
我建议您查看 QSqlQueryModel 文档“详细说明”部分,其中给出了一个示例。为了进一步帮助您,以下是我为应用程序编写的用于迭代模型结果集的一些代码:
for(int i = 0; i < sqlQueryModel->rowCount(); ++i)
{
qDebug() << sqlQueryModel->record(i).value(0).toString();
}
另一种方法是使用函数 QSqlQueryModel::data();我再次建议您查看此处的文档:http://doc-snapshot.qt-project.org/4.8/qsqlquerymodel.html
关于sql - 如何访问从 QSqlquerymodel 集合查询返回的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11650427/