Compare commits

..

19 Commits
v1.0 ... main

Author SHA1 Message Date
a86dfcfe97 新增-#5 添加删除行/列功能 2024-02-24 21:58:57 +08:00
5227ceaf5c 优化-最后一列多增加一个制表符 2024-02-22 22:32:13 +08:00
4bd4ee655d 增加添加列功能 2023-08-13 23:44:42 +08:00
dd9f9408c1 优化右键菜单增加行功能 2023-08-13 22:22:09 +08:00
5744d80e69 增加右键新增行功能 2023-08-13 20:32:19 +08:00
f55de36c25 fix: #3 当某行数据小于最小列数时加载报错 2023-08-08 21:52:23 +08:00
7e7a3e2ab3 Merge branch 'main' of https://begin2019.com:3000/Crimson/TabEditor 2023-07-10 23:41:23 +08:00
6e5383e8f1 fix: #2 新增“保存为”菜单,优化打开逻辑 2023-07-10 23:40:51 +08:00
9a029562f2 新增“保存为”菜单,优化打开逻辑 2023-07-10 23:37:01 +08:00
d59b08a145 更新加载进度条功能 2023-07-10 22:57:48 +08:00
c2e46b264a 更新readme 2023-06-29 20:38:35 +08:00
852ac6d58d 更新打包脚本(自动寻找用户选择目录下的exe程序进行打包) 2023-06-28 22:33:16 +08:00
ea89b96b8e 更新构建脚本 2023-06-27 23:52:43 +08:00
dc97d387ed 更新 'README.md' 2023-06-27 23:28:45 +08:00
28fea51f9a 更新README.md 2023-06-27 23:26:49 +08:00
7e872ef9b4 Merge branch 'main' of https://begin2019.com:3000/Crimson/TabEditor 2023-06-27 23:24:25 +08:00
67dc640c26 txt转sqlite时,若列值超过最大列数则截断 2023-06-27 23:24:06 +08:00
3faf4bc642 更新 'README.md' 2023-06-21 01:04:50 +08:00
e772bd0baf 更新readme 2023-06-21 00:54:43 +08:00
6 changed files with 403 additions and 44 deletions

View File

@ -1,3 +1,40 @@
# TabEditor
这个是一个简约的文本文件编辑工具主要用于编辑以制表符分隔的数据文本文件。与使用Excel编辑相比该程序可以确保文本文件的格式和内容不会发生改变提供更可靠的编辑功能。它简约直观的界面使用户能够轻松地加载、编辑和保存这类文本文件。
## 运行截图
![运行截图](./screenshot/def5f9e083c1f55132797db88b8668f.png)
## TODO
- 自动适配文本文件本身的字符编码格式
以下功能不知道当前用的 tablewidget 是否支持
- 表格内容搜索
- 冻结行、列
- 隐藏行、列
可能需要增加的功能:
- 转换换行符为可见的“\n”——因为由制表符分隔数据的纯文本文件不建议直接使用换行读取改文本的使用场景比较特殊
## Qt程序打包
1. 使用Qt命令行程序Qt 5.14.2 (MinGW 7.3.0 64-bit)
进入Qt程序的build目录下的exe所在路径输入
```shell
windeployqt TabEditor.exe
```
2. 使用此项目中的 [packaging.bat](https://begin2019.com:3000/Crimson/TabEditor-Qt/src/branch/main/packaging.bat) ,双击运行 根据提示操作即可。
> 前提安装了Qt 5.14.2 (MinGW 7.3.0 64-bit)

View File

@ -12,6 +12,7 @@
#include <QInputDialog>
#include <QSqlRecord>
#include <QtDebug>
#include <QContextMenuEvent>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
@ -19,80 +20,214 @@ MainWindow::MainWindow(QWidget *parent)
{
ui->setupUi(this);
setAcceptDrops(true); // 允许窗口接受拖放事件
setAcceptDrops(true);
connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::onOpenButtonClicked);
connect(ui->actionSave, &QAction::triggered, this, &MainWindow::onSaveButtonClicked);
connect(ui->actionSaveAs, &QAction::triggered, this, &MainWindow::onSaveAsButtonClicked);
QIcon openIcon = QApplication::style()->standardIcon(QStyle::SP_DialogOpenButton);
QIcon saveIcon = QApplication::style()->standardIcon(QStyle::SP_DialogSaveButton);
QIcon saveAsIcon = QApplication::style()->standardIcon(QStyle::SP_DialogSaveButton);
ui->actionOpen->setIcon(openIcon);
ui->actionSave->setIcon(saveIcon);
ui->actionSave->setIcon(saveAsIcon);
ui->actionOpen->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N));
ui->actionSave->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
// 添加 "Convert" 菜单
QMenu* convertMenu = menuBar()->addMenu(tr("Convert"));
QMenu* convertMenu = menuBar()->addMenu(tr("转换(&C)"));
// 添加 "Text to SQLite" 子菜单项,并关联槽函数
QAction* textToSqliteAction = convertMenu->addAction(tr("Text to SQLite"));
QAction* textToSqliteAction = convertMenu->addAction(tr("txt 转 SQLite"));
connect(textToSqliteAction, &QAction::triggered, this, &MainWindow::convertTextToSqlite);
// 添加 "SQLite to Text" 子菜单项,并关联槽函数
QAction* sqliteToTextAction = convertMenu->addAction(tr("SQLite to Text"));
QAction* sqliteToTextAction = convertMenu->addAction(tr("SQLite 转 txt"));
connect(sqliteToTextAction, &QAction::triggered, this, &MainWindow::convertSqliteToText);
QIcon convertIcon = QApplication::style()->standardIcon(QStyle::SP_ArrowRight);
textToSqliteAction->setIcon(convertIcon);
sqliteToTextAction->setIcon(convertIcon);
firstColMenu = new QMenu(this);
QAction* addRowAboveAction = firstColMenu->addAction(tr("↑ 向上添加一行"));
QAction* addRowBelowAction = firstColMenu->addAction(tr("向下添加一行 ↓"));
QAction* delRowAction = firstColMenu->addAction(tr("删除"));
connect(addRowAboveAction, &QAction::triggered, this, &MainWindow::onAddRowAboveActionTriggered);
connect(addRowBelowAction, &QAction::triggered, this, &MainWindow::onAddRowBelowActionTriggered);
connect(delRowAction, &QAction::triggered, this, &MainWindow::onDelRowActionTriggered);
firstRowMenu = new QMenu(this);
QAction* addColAboveAction = firstRowMenu->addAction(tr("← 向左添加一列"));
QAction* addColBelowAction = firstRowMenu->addAction(tr("向右添加一列 →"));
QAction* delColAction = firstRowMenu->addAction(tr("删除"));
connect(addColAboveAction, &QAction::triggered, this, &MainWindow::onAddColAboveActionTriggered);
connect(addColBelowAction, &QAction::triggered, this, &MainWindow::onAddColBelowActionTriggered);
connect(delColAction, &QAction::triggered, this, &MainWindow::onDelColActionTriggered);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::contextMenuEvent(QContextMenuEvent* event)
{
QPoint cellPos = ui->tableWidget->viewport()->mapFromGlobal(event->globalPos());
int currentColumn = ui->tableWidget->horizontalHeader()->logicalIndexAt(cellPos.x());
int currentRow = ui->tableWidget->rowAt(cellPos.y());
if (currentColumn == -1 && currentRow == -1) {
// 没有选中行也没有选中列,不显示菜单
return;
}
if (currentColumn == -1 && currentRow >= 0) {
// 选中了行,显示行菜单
ui->tableWidget->selectRow(currentRow);
firstColMenu->popup(event->globalPos());
} else if (currentColumn >= 0 && currentRow == -1) {
// 选中了列,显示列菜单
ui->tableWidget->selectColumn(currentColumn);
firstRowMenu->popup(event->globalPos());
}
}
void MainWindow::onAddRowAboveActionTriggered()
{
QTableWidget* tableWidget = ui->tableWidget;
int currentRow = tableWidget->currentRow();
if (currentRow >= 0)
{
tableWidget->insertRow(currentRow);
}
else
{
// 如果没有选中行,在末尾追加一行
int newRow = tableWidget->rowCount();
tableWidget->insertRow(newRow);
currentRow = newRow;
}
}
void MainWindow::onAddRowBelowActionTriggered()
{
QTableWidget* tableWidget = ui->tableWidget;
int currentRow = tableWidget->currentRow();
if (currentRow >= 0)
{
tableWidget->insertRow(currentRow + 1); // 在下方插入一行
}
else
{
// 如果没有选中行,在末尾追加一行
int newRow = tableWidget->rowCount();
tableWidget->insertRow(newRow);
}
}
void MainWindow::onAddColAboveActionTriggered()
{
QTableWidget* tableWidget = ui->tableWidget;
int currentColumn = tableWidget->currentColumn();
if (currentColumn >= 0)
{
tableWidget->insertColumn(currentColumn);
}
else
{
// 如果没有选中列,在末尾追加一列
int newColumn = tableWidget->columnCount();
tableWidget->insertColumn(newColumn);
currentColumn = newColumn;
}
}
void MainWindow::onAddColBelowActionTriggered()
{
QTableWidget* tableWidget = ui->tableWidget;
int currentColumn = tableWidget->currentColumn();
if (currentColumn >= 0)
{
tableWidget->insertColumn(currentColumn + 1); // 在右侧插入一列
}
else
{
// 如果没有选中列,在末尾追加一列
int newColumn = tableWidget->columnCount();
tableWidget->insertColumn(newColumn);
}
}
void MainWindow::onDelRowActionTriggered()
{
QTableWidget* tableWidget = ui->tableWidget;
int currentRow = tableWidget->currentRow();
tableWidget->removeRow(currentRow);
}
void MainWindow::onDelColActionTriggered()
{
QTableWidget* tableWidget = ui->tableWidget;
int currentColumn = tableWidget->currentColumn();
tableWidget->removeColumn(currentColumn);
}
void MainWindow::showProgressDialog(const QString& labelText, int minimum, int maximum)
{
progressDialog = new QProgressDialog(labelText, "Cancel", minimum, maximum, this);
progressDialog->setWindowModality(Qt::WindowModal);
progressDialog->setAutoClose(false);
progressDialog->setAutoReset(false);
progressDialog->setValue(minimum);
progressDialog->show();
}
void MainWindow::loadTextFile(const QString& fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
// 文件打开失败,处理错误
qDebug() << "Failed to open file:" << fileName;
return;
}
QTextStream in(&file);
// 获取文件大小并预分配内存
qint64 fileSize = file.size();
QString textData;
textData.reserve(fileSize);
// 自动检测并设置正确的文本编码
in.setAutoDetectUnicode(true);
in.setCodec("UTF-16LE");
int tableRow = 0;
int tableCol = 0;
QStringList rows;
// 逐行读取文本数据
while (!in.atEnd())
{
QString line = in.readLine();
textData += line + "\n"; // 保存所有文本数据
tableRow++;
if (tableRow == 2)
if (!line.isEmpty())
{
tableCol = line.split("\t").count(); // 获取第二行的列数
rows.append(line);
tableRow++;
if (tableRow == 2)
{
QStringList columnValues = line.split("\t", QString::SkipEmptyParts);
tableCol = columnValues.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();
showProgressDialog("Converting...", 0, tableRow);
// 将数据显示在表格中
QTableWidget* tableWidget = ui->tableWidget;
QTableWidget* tableWidget = ui->tableWidget;
// 设置表格的行数和列数
tableWidget->setRowCount(tableRow);
@ -100,7 +235,6 @@ void MainWindow::loadTextFile(const QString& fileName)
tableWidget->blockSignals(true); // 阻止信号发射
QStringList rows = textData.split("\n");
for (int i = 0; i < tableRow; ++i)
{
QStringList row = rows.at(i).split("\t");
@ -111,34 +245,87 @@ void MainWindow::loadTextFile(const QString& fileName)
tableWidget->setHorizontalHeaderLabels(row);
}
// 补充空白内容,确保行的列数与表格的列数一致
while (row.count() < tableCol)
{
row.append(""); // 添加空白内容
}
for (int j = 0; j < tableCol; ++j)
{
QTableWidgetItem* item = new QTableWidgetItem(row.at(j));
tableWidget->setItem(i, j, item);
}
progressDialog->setValue(i);
QApplication::processEvents(); // 处理界面事件,使进度条能够更新
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())
openFilePath = QFileDialog::getOpenFileName(this, "Open Text File", "", "Text Files (*.txt)");
if (!openFilePath.isEmpty())
{
loadTextFile(fileName);
loadTextFile(openFilePath);
}
}
void MainWindow::onSaveButtonClicked()
{
if (!openFilePath.isEmpty())
{
QFile file(openFilePath);
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 << "\t"; // 最后一列再加一个制表符,以防万一
out << "\r\n"; // 换行,注意使用\r\n表示换行符
}
file.close();
}
}
}
void MainWindow::onSaveAsButtonClicked()
{
QString fileName = QFileDialog::getSaveFileName(this, "Save Text File", "", "Text Files (*.txt)");
if (!fileName.isEmpty())
@ -175,7 +362,7 @@ void MainWindow::onSaveButtonClicked()
out << "\t"; // 制表符分隔
}
}
out << "\t"; // 最后一列再加一个制表符,以防万一
out << "\r\n"; // 换行,注意使用\r\n表示换行符
}
@ -201,10 +388,10 @@ void MainWindow::dropEvent(QDropEvent *event)
QList<QUrl> urlList = mimeData->urls();
if (urlList.length() == 1)
{
QString fileName = urlList.first().toLocalFile();
if (!fileName.isEmpty())
openFilePath = urlList.first().toLocalFile();
if (!openFilePath.isEmpty())
{
loadTextFile(fileName);
loadTextFile(openFilePath);
}
}
}
@ -251,7 +438,6 @@ void MainWindow::convertTextToSqlite()
columnNames = processedLine.split('\t', Qt::SkipEmptyParts);
}
//QTextStream textStream2(&textFile);
QList<QStringList> data;
textStream.seek(0);
// 读取数据行
@ -259,11 +445,20 @@ void MainWindow::convertTextToSqlite()
{
QString line = textStream.readLine();
QStringList values = line.split("\t");
// 截断行内容,保证不超过列名的个数
if (values.size() > columnNames.size())
{
values = values.mid(0, columnNames.size());
}
data.append(values);
}
textFile.close();
showProgressDialog("Converting...", 0, data.size());
// 打开SQLite数据库
QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName(sqliteFilePath);
@ -300,6 +495,7 @@ void MainWindow::convertTextToSqlite()
insertQuery.chop(2); // 移除最后的逗号和空格
insertQuery += ");";
int progress = 0;
for (const QStringList& row : data)
{
if (row.size() != columnNames.size())
@ -321,8 +517,17 @@ void MainWindow::convertTextToSqlite()
database.close();
return;
}
++progress;
progressDialog->setValue(progress);
QApplication::processEvents();
if (progressDialog->wasCanceled())
{
break;
}
}
progressDialog->hide();
database.commit();
database.close();
@ -378,6 +583,8 @@ void MainWindow::convertSqliteToText()
return;
}
showProgressDialog("Converting...", 0, query.size());
// Open the text file to write data
QFile textFile(textFilePath);
if (!textFile.open(QIODevice::WriteOnly))
@ -397,6 +604,7 @@ void MainWindow::convertSqliteToText()
textStream.setCodec("UTF-16LE");
// Write the data to the text file
int progress = 0;
while (query.next())
{
QSqlRecord record = query.record();
@ -407,9 +615,19 @@ void MainWindow::convertSqliteToText()
if (i < record.count() - 1)
textStream << "\t";
}
textStream << "\t";
textStream << "\r\n";
++progress;
progressDialog->setValue(progress);
QApplication::processEvents();
if (progressDialog->wasCanceled())
{
break;
}
}
progressDialog->hide();
textFile.close();
database.close();

View File

@ -1,9 +1,8 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "qprogressdialog.h"
#include <QThread>
#include <QMainWindow>
#include <QProgressDialog>
#include <QTableWidget>
#include <QDragEnterEvent>
#include <QMimeData>
@ -25,15 +24,26 @@ public:
void dropEvent(QDropEvent *event);
void convertTextToSqlite();
void convertSqliteToText();
void showProgressDialog(const QString& labelText, int minimum, int maximum);
void contextMenuEvent(QContextMenuEvent* event);
private slots:
void onOpenButtonClicked();
void onSaveButtonClicked();
void onSaveAsButtonClicked();
void onAddRowAboveActionTriggered();
void onAddRowBelowActionTriggered();
void onAddColAboveActionTriggered();
void onAddColBelowActionTriggered();
void onDelRowActionTriggered();
void onDelColActionTriggered();
private:
Ui::MainWindow *ui;
QProgressDialog* progressDialog; // 添加进度对话框指针
QMenu* menuFile;
QAction* actionFile;
QProgressDialog* progressDialog;
QString openFilePath;
QMenu* firstColMenu; // 右键菜单
QMenu* firstRowMenu; // 右键菜单
};
#endif // MAINWINDOW_H

View File

@ -20,6 +20,9 @@
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<property name="spacing">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QTableWidget" name="tableWidget"/>
</item>
@ -42,10 +45,10 @@
</property>
<addaction name="actionOpen"/>
<addaction name="actionSave"/>
<addaction name="actionSaveAs"/>
</widget>
<addaction name="menuOpen"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionOpen">
<property name="text">
<string>打开</string>
@ -53,12 +56,18 @@
</action>
<action name="actionSave">
<property name="icon">
<iconset theme="Save"/>
<iconset theme="Save">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>保存</string>
</property>
</action>
<action name="actionSaveAs">
<property name="text">
<string>另存为...</string>
</property>
</action>
</widget>
<resources/>
<connections/>

85
packaging.bat Normal file
View File

@ -0,0 +1,85 @@
@echo off
setlocal
REM 获取当前脚本所在目录
set "SCRIPT_DIR=%~dp0"
REM 构建路径选择
set "BUILD_DIR="
REM 构建类型选择
set "BUILD_TYPE="
REM 列出同级目录中包含 "build" 的文件夹
setlocal enabledelayedexpansion
set "BUILD_INDEX=1"
for /d %%D in ("%SCRIPT_DIR%..\build*") do (
echo [!BUILD_INDEX!] %%~fD
set "BUILD_DIRS[!BUILD_INDEX!]=%%~fD"
set /a "BUILD_INDEX+=1"
)
REM 检查是否找到 build 文件夹
if "%BUILD_INDEX%"=="1" (
echo Error: Build directory not found.
pause
exit /b 1
)
REM 提示用户选择构建目录
set /p "BUILD_SELECTION=Enter the number of the build directory: "
REM 根据用户选择设置构建目录
if defined BUILD_DIRS[%BUILD_SELECTION%] (
set "BUILD_DIR=!BUILD_DIRS[%BUILD_SELECTION%]!"
) else (
echo Error: Invalid build directory selection.
pause
exit /b 1
)
REM 提示用户选择构建类型
echo Choose the build type:
echo [1] Debug
echo [2] Release
set /p "BUILD_TYPE=Enter the number of the build type: "
REM 根据用户选择设置构建目录和构建类型
if "%BUILD_TYPE%"=="1" (
set "BUILD_DIR=%BUILD_DIR%\debug"
) else if "%BUILD_TYPE%"=="2" (
set "BUILD_DIR=%BUILD_DIR%\release"
) else (
echo Error: Invalid build type.
pause
exit /b 1
)
REM 寻找当前要编译的exe程序
set "EXECUTABLE=%BUILD_DIR%\TabEditor.exe"
if not exist "%EXECUTABLE%" (
echo Error: Executable not found in the selected build directory.
pause
exit /b 1
)
REM 切换到Qt环境变量脚本所在目录
cd /d "C:\Qt\Qt5.14.2\5.14.2\mingw73_64\bin"
REM 运行Qt环境变量脚本
call qtenv2.bat
REM 切换到构建目录
cd /d "%BUILD_DIR%"
REM 运行windeployqt命令来打包依赖项
echo Running windeployqt to package dependencies...
windeployqt "%EXECUTABLE%"
REM 切换回脚本所在目录
cd /d "%SCRIPT_DIR%"
REM 完成打包
echo.
echo Packaging completed successfully.
pause

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB