我想将本地数据库(diaBaneData.db)中的数据调用到QML中的TableView中。但我现在正在使用c ++部分。
使用main.cpp中的“model”-Version输出数据库没有问题。使用“model2”版本时,除了标题之外的任何内容都不会显示在QTableView中。由于c ++版本不起作用,qml-tableview也是空的。
我认为角色/字段名一定有问题,但我找不到什么错误。
在输出中会打印几行说:
QSqlQuery::value: unknown field name ''!
的main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include "QTableView"
#include "mysqlmodel.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// Create the database Table inside is named "diaBaneDatabase"
QSqlDatabase m_Database;
m_Database = QSqlDatabase::addDatabase("QSQLITE");
m_Database.setDatabaseName("C:/Users/blade/Desktop/diaBaneData.db");
if (!m_Database.open()) {
qDebug("ERROR");
}
// This is how my table will correctly displayed
QSqlQueryModel* model = new QSqlQueryModel;
model->setQuery("SELECT * FROM diaBaneDatabase");
// Using my own class -> nothing will be shown exepct the headline
QSqlQueryModel* model2 = new MySqlModel();
model2->setQuery("SELECT * FROM diaBaneDatabase");
QTableView *view = new QTableView;
view->setModel(model2);
view->show();
qmlRegisterType<MySqlModel>("MySqlModel", 1, 0, "MySqlModel");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
mysqlmodel.h
#ifndef MYSQLMODEL_H
#define MYSQLMODEL_H
#include <QtSql>
#include <QSqlQuery>
class MySqlModel: public QSqlQueryModel
{
Q_OBJECT
public:
MySqlModel(QObject *parent = 0) : QSqlQueryModel(parent) {}
enum Roles {
id = Qt::UserRole + 1,
dateAndTime,
bloodSugar,
eatenKE,
jubinDextro
};
QHash<int, QByteArray> roleNames() const {
QHash<int, QByteArray> roles;
roles[id] = "idRole";
roles[dateAndTime] = "dateAndTimeRole";
roles[bloodSugar] = "bloodSugarRole";
roles[eatenKE] = "eatenKERole";
roles[jubinDextro] = "jubinDextroRole";
return roles;
}
QVariant data(const QModelIndex &index, int role) const {
if (!index.isValid())
return QVariant();
QString fieldName;
switch (role) {
case id: fieldName = QStringLiteral("diaBaneDatabase.id"); break;
case dateAndTime: fieldName = QStringLiteral("diaBaneDatabase.dateAndTime"); break;
case bloodSugar: fieldName = QStringLiteral("diaBaneDatabase.bloodSugar"); break;
case eatenKE: fieldName = QStringLiteral("diaBaneDatabase.eatenKE"); break;
case jubinDextro: fieldName = QStringLiteral("diaBaneDatabase.jubinDextro"); break;
}
if (!this->record().isGenerated(fieldName))
return QVariant();
else {
QModelIndex item = indexInQuery(index);
if ( !this->query().seek(item.row()) )
return QVariant();
return this->query().value(fieldName);
}
return QVariant();
}
};
#endif // MYSQLMODEL_H
main.qml
import QtQuick 2.5
import QtQuick.Controls 1.4
import MySqlModel 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("&Open")
onTriggered: console.log("Open action triggered");
}
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
}
MySqlModel{
id: sqlTableModel
}
TableView{
id: tableviewTTT
width: 600
height: 400
TableViewColumn{ role: "idRole" ; title: "ID" ; visible: true}
TableViewColumn{ role: "dateAndTimeRole" ; title: "DateAndTime" }
TableViewColumn{ role: "bloodSugarRole" ; title: "BloodSugar" }
TableViewColumn{ role: "eatenKERole" ; title: "EatenKE" }
TableViewColumn{ role: "jubinDextroRole" ; title: "JubinDextro" }
model: sqlTableModel
}
}