• Qt ModelView显示数据库数据


    利用qt的model view来显示数据表userudps里的数据
    在这里插入图片描述
    用了一个label 两个combox和一个tableview,实现如下效果:
    在这里插入图片描述
    我这里用到是mysql数据库,一般配置mysql数据库就两种有驱动或者没驱动,有的话把mysql的bin目录的libmysql.dll复制到qt编译器的bin目录下,没有的话还得装qt的源码,然后编译出mysql的驱动再把mysql的bin目录的libmysql.dll复制到qt编译器的bin目录下。
    main.cpp 我这里用了日志显示一些错误信息

    #include "widget.h"
    #include 
    #include 
    #include 
    #include 
    #include 
    
    void logMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
    {
        // 获取当前时间和日期
        QDateTime currentDateTime = QDateTime::currentDateTime();
    
        // 打开日志文件(追加模式)
        QFile file("log.txt");
        if (!file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
            qDebug() << "Failed to open log file.";
            return;
        }
    
        // 创建文本流,并写入日志信息
        QTextStream stream(&file);
        switch (type) {
        case QtDebugMsg:
            qDebug().noquote() << msg;  // 输出到控制台
            stream << "[" << currentDateTime.toString("yyyy-MM-dd hh:mm:ss") << "] [DEBUG] ";
            break;
        case QtInfoMsg:
            qInfo().noquote() << msg;   // 输出到控制台
            stream << "[" << currentDateTime.toString("yyyy-MM-dd hh:mm:ss") << "] [INFO] ";
            break;
        case QtWarningMsg:
            qWarning().noquote() << msg; // 输出到控制台
            stream << "[" << currentDateTime.toString("yyyy-MM-dd hh:mm:ss") << "] [WARNING] ";
            break;
        case QtCriticalMsg:
            qCritical().noquote() << msg; // 输出到控制台
            stream << "[" << currentDateTime.toString("yyyy-MM-dd hh:mm:ss") << "] [CRITICAL] ";
            break;
        case QtFatalMsg:
            qFatal("%s", msg.toUtf8().constData());
            break;
        default:
            break;
        }
        stream << msg << "\n";
    
        // 关闭日志文件
        file.close();
    }
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        qInstallMessageHandler(logMessage);
        Widget w;
        w.show();
        return a.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    widget.cpp

    #include "widget.h"
    #include "ui_widget.h"
    #include 
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Widget)
    {
        ui->setupUi(this);
        qsqmtableModel=new QSqlQueryModel(this);
        qsqmcomboxModel=new QSqlQueryModel(this);
        qsqmcombox2Model=new QSqlQueryModel(this);
    
    
        QSqlDatabase qsdSqlData=QSqlDatabase::addDatabase("QMYSQL");
        qsdSqlData.setHostName("127.0.0.1");  //数据库服务器IP
        qsdSqlData.setUserName("root");    //数据库用户名
        qsdSqlData.setPassword("123456");  //密码
        qsdSqlData.setDatabaseName("test");  //使用哪个数据库
        if (qsdSqlData.open() == false) {
            qDebug()<<"数据库连接失败";
            return;
        }else{
            qDebug()<<"数据库连接成功";
        }
        QString sqlselect="select* from userudps";
        qsqmtableModel->setQuery(sqlselect);
        ui->tableView->setModel(qsqmtableModel);
        sqlselect="select user_send from userudps group by user_send";
        qsqmcomboxModel->setQuery(sqlselect);
        ui->comboBoxrow->setModel(qsqmcomboxModel);
        ui->comboBoxrow->setCurrentIndex(0);
    
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    void Widget::on_comboBoxrow_currentIndexChanged(const QString &arg1)
    {
        QString sql=QString("select user_recv from userudps where user_send='%1'").arg(arg1);
        qsqmcombox2Model->setQuery(sql);
        ui->comboBoxcol->setModel(qsqmcombox2Model);
        ui->comboBoxcol->setCurrentIndex(0);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include 
    #include 
    #include 
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class Widget; }
    QT_END_NAMESPACE
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        Widget(QWidget *parent = nullptr);
        ~Widget();
    private slots:
        void on_comboBoxrow_currentIndexChanged(const QString &arg1);
    
    private:
        Ui::Widget *ui;
        QSqlQueryModel* qsqmtableModel=nullptr;
        QSqlQueryModel* qsqmcomboxModel=nullptr;
        QSqlQueryModel* qsqmcombox2Model=nullptr;
    };
    #endif // WIDGET_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • 相关阅读:
    Python_文件操作
    【Redis】深入探索 Redis 的数据类型 —— 列表 List
    海康威视工业相机MAC地址
    python测试开发django-198.bootstrap-formvalidation校验成功发ajax请求
    手绘板的制作——手绘(1)
    CRM,下一程在哪?
    GDB常用命令
    2024Java springboot mybatis-flex 根据数据表时间开启定时任务
    uni-app - 面包屑导航组件,支持自定义分隔符,点击可跳转对应页面(全端兼容 H5 APP 小程序,组件代码干净整洁无BUG)
    3D摄影机选择指南,你知道自己需要什么样的摄影机吗?
  • 原文地址:https://blog.csdn.net/weixin_48657573/article/details/133866387