diff --git a/TabEditor.pro b/TabEditor.pro new file mode 100644 index 0000000..d86baaa --- /dev/null +++ b/TabEditor.pro @@ -0,0 +1,26 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++17 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp \ + mainwindow.cpp + +HEADERS += \ + mainwindow.h + +FORMS += \ + mainwindow.ui + +RC_ICONS += app.ico + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/app.ico b/app.ico new file mode 100644 index 0000000..657d7aa Binary files /dev/null and b/app.ico differ diff --git a/fileloader.h b/fileloader.h new file mode 100644 index 0000000..db556d8 --- /dev/null +++ b/fileloader.h @@ -0,0 +1,21 @@ +// fileloader.h +#ifndef FILELOADER_H +#define FILELOADER_H + +#include + +class FileLoader : public QObject +{ + Q_OBJECT +public: + explicit FileLoader(QObject *parent = nullptr); + ~FileLoader(); + +public slots: + void loadTextFile(const QString& fileName); + +signals: + void fileLoaded(QStringList rows, int tableCol); +}; + +#endif // FILELOADER_H diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..53448f2 --- /dev/null +++ b/main.cpp @@ -0,0 +1,12 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.setWindowTitle("TabEditor"); + w.show(); + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..7bf71e6 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,158 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include +#include +#include +#include +#include +#include + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) +{ + ui->setupUi(this); + + connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::onOpenButtonClicked); + connect(ui->actionSave, &QAction::triggered, this, &MainWindow::onSaveButtonClicked); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::loadTextFile(const QString& fileName) +{ + QFile file(fileName); + + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + { + // 文件打开失败,处理错误 + return; + } + + QTextStream in(&file); + + // 获取文件大小并预分配内存 + qint64 fileSize = file.size(); + QString textData; + textData.reserve(fileSize); + + int tableRow = 0; + int tableCol = 0; + while (!in.atEnd()) + { + QString line = in.readLine(); + textData += line + "\n"; // 保存所有文本数据 + tableRow++; + + if (tableRow == 2) + { + tableCol = line.split("\t").count(); // 获取第二行的列数 + } + } + + file.close(); + + progressDialog = new QProgressDialog("Loading...", "Cancel", 0, 0, this); + progressDialog->setWindowModality(Qt::WindowModal); + progressDialog->setAutoClose(false); + progressDialog->setAutoReset(false); + progressDialog->setRange(0, tableRow); + progressDialog->setValue(0); + progressDialog->show(); + + // 将数据显示在表格中 + QTableWidget* tableWidget = ui->tableWidget; + + // 设置表格的行数和列数 + tableWidget->setRowCount(tableRow); + tableWidget->setColumnCount(tableCol); + + tableWidget->blockSignals(true); // 阻止信号发射 + + QStringList rows = textData.split("\n"); + for (int i = 0; i < tableRow; ++i) + { + QStringList row = rows.at(i).split("\t"); + + if (i == 1) + { + // 获取第二行的列名-设置表格的列标题 + tableWidget->setHorizontalHeaderLabels(row); + } + + for (int j = 0; j < tableCol; ++j) + { + QTableWidgetItem* item = new QTableWidgetItem(row.at(j)); + tableWidget->setItem(i, j, item); + } + progressDialog->setValue(i); + QApplication::processEvents(); // 处理界面事件,使进度条能够更新 + if (progressDialog->wasCanceled()) + { + tableWidget->clear(); + break; + } + } + progressDialog->setValue(tableRow); + progressDialog->hide(); + tableWidget->blockSignals(false); // 恢复信号发射 +} + +void MainWindow::onOpenButtonClicked() +{ + QString fileName = QFileDialog::getOpenFileName(this, "Open Text File", "", "Text Files (*.txt)"); + if (!fileName.isEmpty()) + { + loadTextFile(fileName); + } +} + +void MainWindow::onSaveButtonClicked() +{ + QString fileName = QFileDialog::getSaveFileName(this, "Save Text File", "", "Text Files (*.txt)"); + if (!fileName.isEmpty()) + { + QFile file(fileName); + if (file.open(QIODevice::WriteOnly)) + { + // 写入UTF-16带有BOM的标记 + QByteArray bom; + bom.append((char)0xFF); + bom.append((char)0xFE); + file.write(bom); + + QTextStream out(&file); + out.setCodec("UTF-16LE");//Qt6 use: out.setEncoding(QStringConverter::Utf16LE); + + QTableWidget* tableWidget = ui->tableWidget; + int rowCount = tableWidget->rowCount(); + int columnCount = tableWidget->columnCount(); + + for (int i = 0; i < rowCount; ++i) + { + for (int j = 0; j < columnCount; ++j) + { + QTableWidgetItem* item = tableWidget->item(i, j); + if (item != nullptr) + { + QString text = item->text(); + out << text; + } + + if (j != columnCount - 1) + { + out << "\t"; // 制表符分隔 + } + } + + out << "\r\n"; // 换行,注意使用\r\n表示换行符 + } + + file.close(); + QMessageBox::information(this, "保存完成", "文件已成功保存。"); + } + } +} diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..74878dc --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,30 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include "qprogressdialog.h" +#include +#include +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + void loadTextFile(const QString& fileName); + +private slots: + void onOpenButtonClicked(); + void onSaveButtonClicked(); + +private: + Ui::MainWindow *ui; + QProgressDialog* progressDialog; // 添加进度对话框指针 +}; +#endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..3df9490 --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,62 @@ + + + MainWindow + + + + 0 + 0 + 1400 + 800 + + + + MainWindow + + + + Qt::LeftToRight + + + + + + + + + + + + + + + 0 + 0 + 1400 + 21 + + + + + File + + + + + + + + + + Open + + + + + Save + + + + + +