在qml中使用QSqlQueryModel显示数据库数据
环境:
主机: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: parentonClicked: grid.currentIndex = index}}}GridView {id:grid//anchors.fill: parentwidth: parent.widthheight: parent.height - space1.heightanchors {top: space1.bottom;}cellWidth: parent.width * 0.25cellHeight: parent.width * 0.25//model: listModelmodel: myFirstModeldelegate: msnDelegatehighlight: Rectangle { color: "lightsteelblue"; radius: 5 }currentIndex: 2//focus: true}
C++代码:
sqlquerymodel.h
#ifndef SQLQUERYMODEL_H
#define SQLQUERYMODEL_H
#include
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
#include
#include
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操作仅仅运行一次,所以要想实时显示数据库中的数据,能够定时读取数据库以动态显示数据
在qml中使用QSqlQueryModel显示数据库数据
环境:
主机: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: parentonClicked: grid.currentIndex = index}}}GridView {id:grid//anchors.fill: parentwidth: parent.widthheight: parent.height - space1.heightanchors {top: space1.bottom;}cellWidth: parent.width * 0.25cellHeight: parent.width * 0.25//model: listModelmodel: myFirstModeldelegate: msnDelegatehighlight: Rectangle { color: "lightsteelblue"; radius: 5 }currentIndex: 2//focus: true}
C++代码:
sqlquerymodel.h
#ifndef SQLQUERYMODEL_H
#define SQLQUERYMODEL_H
#include
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
#include
#include
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操作仅仅运行一次,所以要想实时显示数据库中的数据,能够定时读取数据库以动态显示数据