Qt之数据写入CSV文件

 

.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 
 7 # The following define makes your compiler emit warnings if you use
 8 # any Qt feature that has been marked deprecated (the exact warnings
 9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12 
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
17 
18 SOURCES += \
19     ZWriteToCSV.cpp \
20     main.cpp \
21     mainwindow.cpp
22 
23 HEADERS += \
24     ZWriteToCSV.h \
25     mainwindow.h
26 
27 FORMS += \
28     mainwindow.ui
29 
30 # Default rules for deployment.
31 qnx: target.path = /tmp/$${TARGET}/bin
32 else: unix:!android: target.path = /opt/$${TARGET}/bin
33 !isEmpty(target.path): INSTALLS += target

 

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }

 

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 
 6 #include "ZWriteToCSV.h"
 7 
 8 QT_BEGIN_NAMESPACE
 9 namespace Ui { class MainWindow; }
10 QT_END_NAMESPACE
11 
12 class MainWindow : public QMainWindow
13 {
14     Q_OBJECT
15 
16 public:
17     MainWindow(QWidget *parent = nullptr);
18     ~MainWindow();
19 
20 private slots:
21     void on_pushButton_clicked();
22 
23     void on_pushButton_2_clicked();
24 
25     void on_pushButton_3_clicked();
26 
27 private:
28     Ui::MainWindow *ui;
29     //
30     ZWriteToCSV *mZWriteToCSV = nullptr;
31 };
32 #endif // MAINWINDOW_H

 

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent)
 5     : QMainWindow(parent)
 6     , ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9     this->setWindowTitle(QStringLiteral("Qt之数据写入CSV文件"));
10 }
11 
12 MainWindow::~MainWindow()
13 {
14     delete ui;
15 }
16 
17 
18 void MainWindow::on_pushButton_clicked()
19 {
20     mZWriteToCSV = new ZWriteToCSV("c:/zhujq/123.csv");
21 }
22 
23 void MainWindow::on_pushButton_2_clicked()
24 {
25     QList<QStringList> oList;
26     for(int i = 0; i < 100; ++i)
27     {
28         QStringList oSList;
29         oSList.push_back("a");
30         oSList.push_back(QString::number(i));
31         oSList.push_back(QStringLiteral(""));
32         oList.push_back(oSList);
33     }
34     mZWriteToCSV->write(oList);
35 }
36 
37 void MainWindow::on_pushButton_3_clicked()
38 {
39     mZWriteToCSV->write(QStringLiteral("新,的,一,行!"));
40 }

 

ZWiteToCSV.h

 1 #ifndef ZWRITETOCSV_H
 2 #define ZWRITETOCSV_H
 3 
 4 #include <QObject>
 5 #include <QFile>
 6 #include <QTextStream>
 7 #include <QDebug>
 8 
 9 class ZWriteToCSV : public QObject
10 {
11 public:
12     ZWriteToCSV(const QString fileName, QObject *parent = nullptr);
13 
14     void write(const QList<QStringList>& data);
15     void write(const QString aData);
16 private:
17     QString mFileName = "";
18     QFile *mFile = nullptr;
19 };
20 
21 
22 
23 #endif // ZWRITETOCSV_H

 

ZWriteToCSV.cpp

 1 #include "ZWriteToCSV.h"
 2 
 3 ZWriteToCSV::ZWriteToCSV(const QString fileName, QObject *parent)
 4     : QObject(parent)
 5     , mFileName(fileName)
 6 {
 7     //
 8     mFile = new QFile(mFileName);
 9 }
10 
11 void ZWriteToCSV::write(const QList<QStringList> &data)
12 {
13     if(mFile)
14     {
15         if (!mFile->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
16         {
17             qDebug() << "Failed to open file for writing:" << mFile->errorString();
18             return;
19         }
20 
21         QTextStream stream(mFile);
22         stream.setCodec("UTF-8"); //
23 
24         for (const QStringList& row : data)
25         {
26             for (int i = 0; i < row.size(); ++i)
27             {
28                 stream << row.at(i);
29                 if (i != row.size() - 1)
30                 {
31                     stream << ",";
32                 }
33             }
34             stream << endl;
35         }
36 
37         mFile->close();
38     }
39 }
40 
41 void ZWriteToCSV::write(const QString aData)
42 {
43     if(mFile)
44     {
45         if (!mFile->open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Append))
46         {
47             qDebug() << "Failed to open file for writing:" << mFile->errorString();
48             return;
49         }
50 
51         QTextStream stream(mFile);
52         stream.setCodec("UTF-8"); //
53         stream << aData;
54         stream << endl;
55         mFile->close();
56     }
57 }

 mainwindow.ui

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <ui version="4.0">
 3  <class>MainWindow</class>
 4  <widget class="QMainWindow" name="MainWindow">
 5   <property name="geometry">
 6    <rect>
 7     <x>0</x>
 8     <y>0</y>
 9     <width>359</width>
10     <height>216</height>
11    </rect>
12   </property>
13   <property name="windowTitle">
14    <string>MainWindow</string>
15   </property>
16   <widget class="QWidget" name="centralwidget">
17    <widget class="QPushButton" name="pushButton">
18     <property name="geometry">
19      <rect>
20       <x>80</x>
21       <y>30</y>
22       <width>200</width>
23       <height>30</height>
24      </rect>
25     </property>
26     <property name="text">
27      <string>联接文件</string>
28     </property>
29    </widget>
30    <widget class="QPushButton" name="pushButton_2">
31     <property name="geometry">
32      <rect>
33       <x>80</x>
34       <y>90</y>
35       <width>200</width>
36       <height>30</height>
37      </rect>
38     </property>
39     <property name="text">
40      <string>写入全部内容</string>
41     </property>
42    </widget>
43    <widget class="QPushButton" name="pushButton_3">
44     <property name="geometry">
45      <rect>
46       <x>80</x>
47       <y>150</y>
48       <width>200</width>
49       <height>30</height>
50      </rect>
51     </property>
52     <property name="text">
53      <string>写入一行内容</string>
54     </property>
55    </widget>
56   </widget>
57  </widget>
58  <resources/>
59  <connections/>
60 </ui>

 

posted on 2026-01-31 16:26  疯狂delphi  阅读(0)  评论(0)    收藏  举报

导航