• 用C++ QT实现点击下拉框,选则后自动填写其他项


    一 . 读取选项内容

    1.1 从csv文件读取

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        // 打开CSV文件
        QFile file("your_csv_file.csv");
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            qDebug() << "无法打开CSV文件";
            return 1;
        }
    
        // 创建一个QVector来存储CSV数据
        QVector<QStringList> csvData;
    
        // 读取并解析CSV文件
        QTextStream in(&file);
        while (!in.atEnd())
        {
            QString line = in.readLine();
            QStringList fields = line.split(",");
    
            // 将CSV行数据添加到QVector
            csvData.append(fields);
        }
    
        // 输出CSV数据
        for (const QStringList &row : csvData)
        {
            qDebug() << row.join(", ");
        }
    
        // 关闭文件
        file.close();
    
        return app.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

    excel中读取

    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        // 创建一个Qt应用程序
        QWidget window;
        window.setWindowTitle("读取Excel文件");
    
        // 创建一个QTableWidget来显示Excel数据
        QTableWidget *tableWidget = new QTableWidget(&window);
    
        // 创建QAxObject以连接到Excel应用程序
        QAxObject excel("Excel.Application", &window);
        excel.setProperty("Visible", false); // 使Excel不可见
    
        // 打开Excel工作簿
        QAxObject *workbooks = excel.querySubObject("Workbooks");
        QAxObject *workbook = workbooks->querySubObject("Open(const QString&)", "your_excel_file.xlsx");
    
        if (workbook)
        {
            // 获取第一个工作表
            QAxObject *worksheets = workbook->querySubObject("Worksheets");
            QAxObject *worksheet = worksheets->querySubObject("Item(int)", 1);
    
            if (worksheet)
            {
                // 获取行数和列数
                int rowCount = worksheet->property("UsedRange").querySubObject("Rows")->property("Count").toInt();
                int columnCount = worksheet->property("UsedRange").querySubObject("Columns")->property("Count").toInt();
    
                // 设置表格的行数和列数
                tableWidget->setRowCount(rowCount);
                tableWidget->setColumnCount(columnCount);
    
                // 读取Excel数据并填写到表格中
                for (int row = 1; row <= rowCount; ++row)
                {
                    for (int col = 1; col <= columnCount; ++col)
                    {
                        QAxObject *cell = worksheet->querySubObject("Cells(int, int)", row, col);
                        QString cellData = cell->dynamicCall("Value()").toString();
                        QTableWidgetItem *item = new QTableWidgetItem(cellData);
                        tableWidget->setItem(row - 1, col - 1, item);
                    }
                }
    
                // 释放工作表对象
                worksheet->dynamicCall("Release()");
            }
    
            // 释放工作簿对象
            workbook->dynamicCall("Close()");
            workbook->dynamicCall("Release()");
        }
    
        // 退出Excel应用程序
        excel.dynamicCall("Quit()");
    
        // 显示表格
        tableWidget->show();
    
        return app.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
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    auto cbox = new ComboBox();
        cbox->setEditable(true);
        m_pTableView->setIndexWidget(m_pModel->index(idx, 0), cbox);
        // 获取文件中的数据
        QFile file("D:/develop/Projects/QTPro/TMI/tmi/MaterialInfo.csv");
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            qDebug() << "无法打开CSV文件" << endl;
            return;
        }
    
    //     创建一个QVector来存储CSV数据
    //        QVector csvData;
    
        // 读取并解析CSV文件
        QTextStream in(&file);
        cbox->clear();
        while (!in.atEnd()) {
            QString line = in.readLine();
            QStringList fields = line.split(",");
            cbox->addItem(fields[0]);
            // 将CSV行数据添加到QVector
            //        csvData.append(fields);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    神舟电脑怎么清理缓存文件?介绍几种简单有效方法
    FSAF:嵌入anchor-free分支来指导acnhor-based算法训练 | CVPR2019
    解压apk后各文件夹含义
    acwing算法基础之基础算法--高精度除法算法
    ▶《强化学习的数学原理》(2024春)_西湖大学赵世钰 Ch1 基本概念
    猿如意 | 带你手把手安装 Visual Studio Code
    关于在CentOS服务器上采用pm2 部署node 服务
    【Matlab】常用函数汇总(一)
    华为H12-831题库
    TortoiseGit安装和配置详细说明
  • 原文地址:https://blog.csdn.net/qq_42817360/article/details/133982166