首页 > 其他分享 >Qt进行CSV文件操作

Qt进行CSV文件操作

时间:2022-12-20 14:00:24浏览次数:68  
标签:文件 Widget csv Qt csvOutPut lt QString file CSV

https://blog.csdn.net/weixin_39935783/article/details/111685444?ops_request_misc=&request_id=&biz_id=102&utm_term=qt csv文件&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-0-111685444.142v68pc_rank_34_queryrelevant25,201v4add_ask,213v2t3_control1&spm=1018.2226.3001.4187

**

Qt操作csv文件

**

csv文件简述
逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符。

示例代码

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();

private slots:
void onBtn();
void onOpenBtn();

private:
Ui::Widget *ui;
};

#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

信号绑定

connect(ui->pushButton, &QPushButton::clicked, this, &Widget::onBtn);
connect(ui->pushButton_open, &QPushButton::clicked, this, &Widget::onOpenBtn);
  • 1
  • 2

此信号与槽函数的关联在构造函数中实现即可。

槽函数:onBtn

QString csvFileName = QFileDialog::getSaveFileName(this, QStringLiteral("保存文件设置"), ".", "csv files(*.csv)");
    QFile file(csvFileName);
    if(!file.exists())
    {
        file.open(QIODevice::WriteOnly);
        QTextStream csvOutPut(&file);
        QString str ="姓名,班级,年龄\n";
        csvOutPut << str.toUtf8();
        file.close();
    }
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>file<span class="token punctuation">.</span><span class="token function">open</span><span class="token punctuation">(</span>QIODevice<span class="token operator">::</span>WriteOnly <span class="token operator">|</span> QIODevice<span class="token operator">::</span>Append<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
    <span class="token function">qDebug</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&lt;&lt;</span> <span class="token string">"0000000000000000000000"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span>
<span class="token punctuation">{<!-- --></span>
    QTextStream <span class="token function">csvOutPut</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>
    QString str1 <span class="token operator">=</span> <span class="token string">"张三,3年级1班,28\n"</span><span class="token punctuation">;</span>
    QString str2 <span class="token operator">=</span> <span class="token string">"李四,3年级2班,36\n"</span><span class="token punctuation">;</span>
    QString str3 <span class="token operator">=</span> <span class="token string">"王五,3年级2班,36\n"</span><span class="token punctuation">;</span>

    csvOutPut <span class="token operator">&lt;&lt;</span> str1<span class="token punctuation">.</span><span class="token function">toUtf8</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    csvOutPut <span class="token operator">&lt;&lt;</span> str2<span class="token punctuation">.</span><span class="token function">toUtf8</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    csvOutPut <span class="token operator">&lt;&lt;</span> str3<span class="token punctuation">.</span><span class="token function">toUtf8</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    file<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    file<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
  • 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

运行结果:
在这里插入图片描述
在这里插入图片描述
槽函数:onOpenBtn

QString fileName = QFileDialog::getOpenFileName(this,QStringLiteral("打开文件"),".",tr("csv files(*.csv)"));
    if(fileName == "")
    {
        return;
    }
QFile <span class="token function">file</span><span class="token punctuation">(</span>fileName<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>file<span class="token punctuation">.</span><span class="token function">open</span><span class="token punctuation">(</span>QIODevice<span class="token operator">::</span>ReadOnly<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
    <span class="token function">qDebug</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&lt;&lt;</span><span class="token function">QStringLiteral</span><span class="token punctuation">(</span><span class="token string">"请正确选择csv文件"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span>
<span class="token punctuation">{<!-- --></span>
    QTextStream <span class="token operator">*</span> read <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">QTextStream</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>
    QStringList Data <span class="token operator">=</span> read<span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">readAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">"\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>            <span class="token comment">//每行以\n区分</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span> i <span class="token operator">&lt;</span> Data<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span> <span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>               <span class="token comment">//去掉最后一行空白行</span>
    <span class="token punctuation">{<!-- --></span>
         QStringList strLine <span class="token operator">=</span> Data<span class="token punctuation">.</span><span class="token function">at</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">","</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment">//一行中的单元格以,区分</span>
         <span class="token function">qDebug</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&lt;&lt;</span> strLine<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

运行结果:
在这里插入图片描述

</article>

标签:文件,Widget,csv,Qt,csvOutPut,lt,QString,file,CSV
From: https://www.cnblogs.com/mkmkbj/p/16994033.html

相关文章

  • pyqt5图书管理系统--8、学生页面设计、借阅状态页面设计和所有书籍页面设计
    本节分为三个部分:学生页面、借阅状态页面和所有书籍查询页面。主要流程:1、学生用户主界面,可以使用借阅书籍、归还书籍、查看借阅状态、查看所有书籍信息。     2、......
  • PyQt 界面设计要点
    1.需要先设计布局常规的布局有以下几种:​​QVBoxLayout​​,​​QHBoxLayout​​,​​QGridBoxLayout​​通过情况下的做法是采用QVBoxLayout作为基础的布局之后采用QH......
  • robocopy超快小文件复制工具
    robocopyi:\.  d:\oldd/e/mir/mt:120/xd“$recycle.bing”/xd“systemvolumeinformation”/r:10/w:10”mt120 表示以120线程复制robocopy 源 目标  robocop......
  • pyqt系列【一】个人常用工具代码
    pyqt系列开篇。在使用pyqt写上位机软件时,经常会有很多重复的通用的代码,在这里整理记录。字体库在不同的平台使用各平台自带的效果比较好的字体,并调整字号。classFontT......
  • 【Selenium : 文件操作】
    一、前言文件操作是Web端自动化中比较常用的一个操作,一般文件操作包含:上传、下载WebDriver仅仅提供了下载文件相关的API,上传文件的API并没实现,需要我们自己去实现;而且......
  • jsconfig.json 配置vscode只能跳转文件
    -根目录创建jsconfig.json{"compilerOptions":{"baseUrl":"./","paths":{"@/*":["src/*"],}},"include":["src/**/*"],......
  • mongodb数据库修复 mongodb数据库丢失恢复 mongodb数据库数据恢复 mongodb数据库文件0
    mongodb数据库修复mongodb数据库丢失恢复mongodb数据库数据恢复mongodb数据库文件0kb数据恢复客户名称保密数据类型mongodb3.0数据容量20GB故障类型强制重启服务器......
  • linux 中如何查找在过去一段时间内修改过的文件
     001、查找在过去五分钟内修改过的文件find./-mmin-5 002、查找在过去10分钟内修改过的文件find./-mmin-10 003、查找在过去一天内修改过的文件......
  • git 看某次commit改了哪些文件
    gitlog查看commit的历史gitshow查看某次commit的修改内容gitlog-p查看某个文件的修改历史gitlog-p-2查看最近2次的更新内容gitlog–name-status每次修改......
  • DataGrip导出数据库sql文件
    选中一个数据库中指定数据表,然后单击右键,选择Exportwithmysqldump如果显示如图所示错误(路径不存在),则在终端里输入whichmysqldump,然后去替代默认路径即可。......