QML与C++交互:在qml中使用QSqlQueryModel显示数据库数据
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
参考链接:
http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML
环境:
主机:WIN7
开发环境:Qt5.2.1
说明:
在QML中不能直接对数据库进行操作,所以将QSqlQueryModel封装成子类,作为属性给QML使用
效果图:
源代码:
qml文件中负责数据托管显示的代码:
- Component
- {
- id: msnDelegate
- Item
- {
- id: wrapper
- width: grid.cellWidth; height: grid.cellHeight
- Column
- {
- Image{ source: "pics/light_on.png";anchors.horizontalCenter: parent.horizontalCenter; width: grid.cellWidth * 0.7; height: grid.cellHeight * 0.7}
- Text { text: ctrl_id;anchors.horizontalCenter: parent.horizontalCenter; color: wrapper.GridView.isCurrentItem ? "red" :"blue" }
- Text { text: name;anchors.horizontalCenter: parent.horizontalCenter; color: wrapper.GridView.isCurrentItem ? "red" :"blue" }
- }
- MouseArea
- {
- anchors.fill: parent
- onClicked: grid.currentIndex = index
- }
- }
- }
- GridView {
- id:grid
- //anchors.fill: parent
- width: parent.width
- height: parent.height - space1.height
- anchors {top: space1.bottom;}
- cellWidth: parent.width * 0.25
- cellHeight: parent.width * 0.25
- //model: listModel
- model: myFirstModel
- delegate: msnDelegate
- highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
- currentIndex: 2
- //focus: true
- }
C++代码:
sqlquerymodel.h
- #ifndef SQLQUERYMODEL_H
- #define SQLQUERYMODEL_H
- #include <QSqlQueryModel>
- class SqlQueryModel : public QSqlQueryModel
- {
- Q_OBJECT
- void generateRoleNames();
- public:
- explicit SqlQueryModel(QObject *parent = 0);
- void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase());
- void setQuery(const QSqlQuery &query);
- QVariant data(const QModelIndex &index, int role) const;
- virtual QHash<int, QByteArray> roleNames() const;
- signals:
- public slots:
- };
- #endif // SQLQUERYMODEL_H
sqlquerymodel.cpp
- #include "sqlquerymodel.h"
- #include <QSqlRecord>
- #include <QSqlField>
- #include <QDebug>
- SqlQueryModel::SqlQueryModel(QObject *parent) :
- QSqlQueryModel(parent)
- {
- }
- void SqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db)
- {
- QSqlQueryModel::setQuery(query,db);
- generateRoleNames();
- }
- void SqlQueryModel::setQuery(const QSqlQuery & query)
- {
- QSqlQueryModel::setQuery(query);
- generateRoleNames();
- }
- void SqlQueryModel::generateRoleNames()
- {
- QHash<int, QByteArray> roleNames;
- for( int i = 0; i < record().count(); i++) {
- roleNames[Qt::UserRole + i + 1] = record().fieldName(i).toUtf8();
- }
- //setRoleNames(roleNames);
- }
- QHash<int, QByteArray> SqlQueryModel::roleNames() const
- {
- QHash<int, QByteArray> roleNames;
- for( int i = 0; i < record().count(); i++) {
- roleNames[Qt::UserRole + i + 1] = record().fieldName(i).toUtf8();
- }
- return roleNames;
- }
- QVariant SqlQueryModel::data(const QModelIndex &index, int role) const
- {
- QVariant value = QSqlQueryModel::data(index, role);
- 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;
- }
主函数中 数据关联的代码:
- SqlQueryModel *model1 = new SqlQueryModel(0);
- model1->setQuery("SELECT * FROM ctrl_para");
- QtQuick2ApplicationViewer viewer;
- viewer.rootContext()->setContextProperty("myFirstModel", model1);
- viewer.setMainQmlFile(QStringLiteral("qml/SH_User/base.qml"));
- viewer.showExpanded();
注意:
query操作只执行一次,所以要想实时显示数据库中的数据,可以定时读取数据库以动态显示数据