first comit
This commit is contained in:
parent
55b32933e3
commit
eb351c0fa1
26
TabEditor.pro
Normal file
26
TabEditor.pro
Normal file
|
@ -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
|
21
fileloader.h
Normal file
21
fileloader.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
// fileloader.h
|
||||
#ifndef FILELOADER_H
|
||||
#define FILELOADER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
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
|
12
main.cpp
Normal file
12
main.cpp
Normal file
|
@ -0,0 +1,12 @@
|
|||
#include "mainwindow.h"
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
MainWindow w;
|
||||
w.setWindowTitle("TabEditor");
|
||||
w.show();
|
||||
return a.exec();
|
||||
}
|
158
mainwindow.cpp
Normal file
158
mainwindow.cpp
Normal file
|
@ -0,0 +1,158 @@
|
|||
#include "mainwindow.h"
|
||||
#include "ui_mainwindow.h"
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
#include <QTableWidget>
|
||||
#include <QFileDialog>
|
||||
#include <QProgressDialog>
|
||||
#include <QMessageBox>
|
||||
|
||||
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, "保存完成", "文件已成功保存。");
|
||||
}
|
||||
}
|
||||
}
|
30
mainwindow.h
Normal file
30
mainwindow.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include "qprogressdialog.h"
|
||||
#include <QThread>
|
||||
#include <QMainWindow>
|
||||
#include <QTableWidget>
|
||||
|
||||
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
|
62
mainwindow.ui
Normal file
62
mainwindow.ui
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>MainWindow</class>
|
||||
<widget class="QMainWindow" name="MainWindow">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1400</width>
|
||||
<height>800</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>MainWindow</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralwidget">
|
||||
<property name="layoutDirection">
|
||||
<enum>Qt::LeftToRight</enum>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QTableWidget" name="tableWidget"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QMenuBar" name="menubar">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1400</width>
|
||||
<height>21</height>
|
||||
</rect>
|
||||
</property>
|
||||
<widget class="QMenu" name="menuOpen">
|
||||
<property name="title">
|
||||
<string>File</string>
|
||||
</property>
|
||||
<addaction name="actionOpen"/>
|
||||
<addaction name="actionSave"/>
|
||||
</widget>
|
||||
<addaction name="menuOpen"/>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusbar"/>
|
||||
<action name="actionOpen">
|
||||
<property name="text">
|
||||
<string>Open</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionSave">
|
||||
<property name="text">
|
||||
<string>Save</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
Loading…
Reference in New Issue
Block a user