QML与C++交互:在qml中使用QSqlQueryModel显示数据库数据

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文件中负责数据托管显示的代码:

[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Component  
  2.         {  
  3.             id: msnDelegate  
  4.             Item  
  5.             {  
  6.                 id: wrapper  
  7.                 width: grid.cellWidth; height: grid.cellHeight  
  8.                 Column  
  9.                 {  
  10.                     Image{ source: "pics/light_on.png";anchors.horizontalCenter: parent.horizontalCenter; width: grid.cellWidth * 0.7; height: grid.cellHeight * 0.7}  
  11.                     Text { text: ctrl_id;anchors.horizontalCenter: parent.horizontalCenter; color: wrapper.GridView.isCurrentItem ? "red" :"blue" }  
  12.                     Text { text: name;anchors.horizontalCenter: parent.horizontalCenter; color: wrapper.GridView.isCurrentItem ? "red" :"blue" }  
  13.                 }  
  14.   
  15.                 MouseArea  
  16.                 {  
  17.                     anchors.fill: parent  
  18.                     onClicked: grid.currentIndex = index  
  19.                 }  
  20.             }  
  21.         }  
  22.         GridView {  
  23.             id:grid  
  24.             //anchors.fill: parent  
  25.             width: parent.width  
  26.             height: parent.height - space1.height  
  27.             anchors {top: space1.bottom;}  
  28.             cellWidth: parent.width * 0.25  
  29.             cellHeight: parent.width * 0.25  
  30.             //model: listModel  
  31.             model: myFirstModel  
  32.             delegate: msnDelegate  
  33.             highlight: Rectangle { color: "lightsteelblue"; radius: 5 }  
  34.             currentIndex: 2  
  35.             //focus: true  
  36.         }  

C++代码:

sqlquerymodel.h

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #ifndef SQLQUERYMODEL_H  
  2. #define SQLQUERYMODEL_H  
  3.   
  4. #include <QSqlQueryModel>  
  5.   
  6. class SqlQueryModel : public QSqlQueryModel  
  7. {  
  8.     Q_OBJECT  
  9.   
  10.     void generateRoleNames();  
  11.   
  12. public:  
  13.     explicit SqlQueryModel(QObject *parent = 0);  
  14.   
  15.     void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase());  
  16.     void setQuery(const QSqlQuery &query);  
  17.     QVariant data(const QModelIndex &index, int role) const;  
  18.   
  19.     virtual QHash<int, QByteArray> roleNames() const;  
  20.   
  21. signals:  
  22.   
  23. public slots:  
  24.   
  25. };  
  26.   
  27. #endif // SQLQUERYMODEL_H  

sqlquerymodel.cpp

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "sqlquerymodel.h"  
  2. #include <QSqlRecord>  
  3. #include <QSqlField>  
  4. #include <QDebug>  
  5.   
  6. SqlQueryModel::SqlQueryModel(QObject *parent) :  
  7.     QSqlQueryModel(parent)  
  8. {  
  9.   
  10. }  
  11.   
  12. void SqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db)  
  13. {  
  14.     QSqlQueryModel::setQuery(query,db);  
  15.     generateRoleNames();  
  16. }  
  17.   
  18. void SqlQueryModel::setQuery(const QSqlQuery & query)  
  19. {  
  20.     QSqlQueryModel::setQuery(query);  
  21.     generateRoleNames();  
  22. }  
  23.   
  24. void SqlQueryModel::generateRoleNames()  
  25. {  
  26.     QHash<int, QByteArray> roleNames;  
  27.     forint i = 0; i < record().count(); i++) {  
  28.         roleNames[Qt::UserRole + i + 1] = record().fieldName(i).toUtf8();  
  29.     }  
  30.     //setRoleNames(roleNames);  
  31. }  
  32.   
  33. QHash<int, QByteArray> SqlQueryModel::roleNames() const  
  34. {  
  35.     QHash<int, QByteArray> roleNames;  
  36.     forint i = 0; i < record().count(); i++) {  
  37.         roleNames[Qt::UserRole + i + 1] = record().fieldName(i).toUtf8();  
  38.     }  
  39.     return roleNames;  
  40. }  
  41.   
  42. QVariant SqlQueryModel::data(const QModelIndex &index, int role) const  
  43. {  
  44.     QVariant value = QSqlQueryModel::data(index, role);  
  45.     if(role < Qt::UserRole)  
  46.     {  
  47.         value = QSqlQueryModel::data(index, role);  
  48.     }  
  49.     else  
  50.     {  
  51.         int columnIdx = role - Qt::UserRole - 1;  
  52.         QModelIndex modelIndex = this->index(index.row(), columnIdx);  
  53.         value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);  
  54.     }  
  55.     return value;  
  56. }  

主函数中 数据关联的代码:
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. SqlQueryModel *model1 = new SqlQueryModel(0);  
  2. model1->setQuery("SELECT * FROM ctrl_para");  
  3.   
  4. QtQuick2ApplicationViewer viewer;  
  5.   
  6. viewer.rootContext()->setContextProperty("myFirstModel", model1);  
  7.   
  8. viewer.setMainQmlFile(QStringLiteral("qml/SH_User/base.qml"));  
  9. viewer.showExpanded();  

注意:

query操作只执行一次,所以要想实时显示数据库中的数据,可以定时读取数据库以动态显示数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值