我想从我的SQlite数据库中获取模型,当我在QSqlQueryModel中设置查询时,我可以获得行数,列数等。但是每列中的数据我都有一个ReferenceError试图获取列的名称 cpp代码:
//data base users
QSqlQueryModel *sqlModel = new QSqlQueryModel();
sqlModel->setQuery("SELECT usuarios.nombre FROM usuarios");
sqlModel->setHeaderData(0,Qt::Horizontal, QObject::tr("nombre"));
qDebug() << "ROL: " << sqlModel->roleNames();
qDebug() << "number of rows: " << sqlModel->rowCount();
qDebug() << "number of columns: " << sqlModel->columnCount();
qDebug() << "HEADER: " << sqlModel->headerData(1, Qt::Horizontal).toString();
这就是我在QML中的ListView中所拥有的:
ListView {
id: listaUsuarios
model: sqlModel
delegate:
Text{
anchors.fill: parent
text: nombre
}
}
答案 0 :(得分:1)
如何观察表的字段不是角色,因此无法从QML访问它们,因此要访问时,必须将字段的名称添加为角色,为此必须覆盖该类:< / p>
class SqlQueryModel: public QSqlQueryModel{
public:
using QSqlQueryModel::QSqlQueryModel;
QVariant data(const QModelIndex &index, int role) const
{
QVariant value;
if (index.isValid()) {
if (role < Qt::UserRole) {
value = QSqlQueryModel::data(index, role);
} else {
int columnIdx = role - Qt::UserRole - 1;
QModelIndex modelIndex = this->index(index.row(), columnIdx);
value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}
}
return value;
}
QHash<int, QByteArray> roleNames() const
{
QHash<int, QByteArray> roles = QSqlQueryModel::roleNames();
for (int i = 0; i < this->record().count(); i ++) {
roles.insert(Qt::UserRole + i + 1, record().fieldName(i).toUtf8());
}
return roles;
}
};
示例:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSqlRecord>
#include <QDebug>
static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open()) {
qDebug()<<"Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit.";
return false;
}
QSqlQuery query;
query.exec("create table usuarios (ID INTEGER PRIMARY KEY AUTOINCREMENT, "
"nombre VARCHAR(20), apellido VARCHAR(20))");
query.exec("insert into usuarios values(1, 'Danny', 'Young')");
query.exec("insert into usuarios values(2, 'Christine', 'Holand')");
query.exec("insert into usuarios values(3, 'Lars', 'Gordon')");
query.exec("insert into usuarios values(4, 'Roberto', 'Robitaille')");
query.exec("insert into usuarios values(5, 'Maria', 'Papadopoulos')");
return true;
}
class SqlQueryModel: public QSqlQueryModel{
public:
using QSqlQueryModel::QSqlQueryModel;
QVariant data(const QModelIndex &index, int role) const
{
QVariant value;
if (index.isValid()) {
if (role < Qt::UserRole) {
value = QSqlQueryModel::data(index, role);
} else {
int columnIdx = role - Qt::UserRole - 1;
QModelIndex modelIndex = this->index(index.row(), columnIdx);
value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}
}
return value;
}
QHash<int, QByteArray> roleNames() const
{
QHash<int, QByteArray> roles = QSqlQueryModel::roleNames();
for (int i = 0; i < this->record().count(); i ++) {
roles.insert(Qt::UserRole + i + 1, record().fieldName(i).toUtf8());
}
return roles;
}
};
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
if(!createConnection())
return -1;
SqlQueryModel sqlModel;
sqlModel.setQuery("SELECT usuarios.nombre FROM usuarios");
qDebug() << sqlModel.roleNames();
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("sqlModel", &sqlModel);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
输出: