博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QML与C++交互 在qml中使用QSqlQueryModel显示数据库数据
阅读量:3949 次
发布时间:2019-05-24

本文共 3766 字,大约阅读时间需要 12 分钟。

               

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

本文博客链接:,作者:jdh,转载请注明.

参考链接:

环境:

主机: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 
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
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
roleNames;    for( int i = 0; i < record().count(); i++) {        roleNames[Qt::UserRole + i + 1] = record().fieldName(i).toUtf8();    }    //setRoleNames(roleNames);}QHash
SqlQueryModel::roleNames() const{    QHash
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操作只执行一次,所以要想实时显示数据库中的数据,可以定时读取数据库以动态显示数据

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!

你可能感兴趣的文章
Using ViewPager for Screen Slides 使用屏幕幻灯片ViewPager
查看>>
Displaying Card Flip Animations 显示卡片翻转动画
查看>>
Zooming a View 缩放视图
查看>>
Animating Layout Changes 动画布局的更改
查看>>
Controlling Your App’s Volume and Playback 控制应用程序的音量和播放
查看>>
Managing Audio Focus 管理音频焦点
查看>>
Dealing with Audio Output Hardware 处理音频输出硬件设备
查看>>
Monitoring the Battery Level and Charging State 监测电池电量和充电状态
查看>>
Determining and Monitoring the Docking State and Type 判断并监测设备的停驻状态与类型
查看>>
Determining and Monitoring the Connectivity Status 根据网络连接状况去省电
查看>>
Manipulating Broadcast Receivers On Demand 按需操控广播接收
查看>>
Creating a View Class 创建一个视图类
查看>>
Custom Drawing 自定义绘制
查看>>
Making the View Interactive 视图互动
查看>>
Optimizing the View 优化视图
查看>>
Setting Up the Search Interface 设置搜索界面
查看>>
Storing and Searching for Data 数据存储和搜索
查看>>
Remaining Backward Compatible 保持向后兼容
查看>>
Remembering Your User 记住你的用户
查看>>
Authenticating to OAuth2 Services 验证OAuth2服务
查看>>