新增“保存为”菜单,优化打开逻辑

This commit is contained in:
阿甘 2023-07-10 23:37:01 +08:00
parent d59b08a145
commit 9a029562f2
3 changed files with 84 additions and 19 deletions

View File

@ -23,12 +23,15 @@ MainWindow::MainWindow(QWidget *parent)
connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::onOpenButtonClicked); connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::onOpenButtonClicked);
connect(ui->actionSave, &QAction::triggered, this, &MainWindow::onSaveButtonClicked); 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 openIcon = QApplication::style()->standardIcon(QStyle::SP_DialogOpenButton);
QIcon saveIcon = QApplication::style()->standardIcon(QStyle::SP_DialogSaveButton); QIcon saveIcon = QApplication::style()->standardIcon(QStyle::SP_DialogSaveButton);
QIcon saveAsIcon = QApplication::style()->standardIcon(QStyle::SP_DialogSaveButton);
ui->actionOpen->setIcon(openIcon); ui->actionOpen->setIcon(openIcon);
ui->actionSave->setIcon(saveIcon); ui->actionSave->setIcon(saveIcon);
ui->actionSave->setIcon(saveAsIcon);
ui->actionOpen->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N)); ui->actionOpen->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N));
ui->actionSave->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); ui->actionSave->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
@ -69,28 +72,34 @@ void MainWindow::loadTextFile(const QString& fileName)
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{ {
// 文件打开失败,处理错误 qDebug() << "Failed to open file:" << fileName;
return; return;
} }
QTextStream in(&file); QTextStream in(&file);
// 获取文件大小并预分配内存 // 自动检测并设置正确的文本编码
qint64 fileSize = file.size(); in.setAutoDetectUnicode(true);
QString textData; in.setCodec("UTF-16LE");
textData.reserve(fileSize);
int tableRow = 0; int tableRow = 0;
int tableCol = 0; int tableCol = 0;
QStringList rows;
// 逐行读取文本数据
while (!in.atEnd()) while (!in.atEnd())
{ {
QString line = in.readLine(); QString line = in.readLine();
textData += line + "\n"; // 保存所有文本数据 if (!line.isEmpty())
tableRow++;
if (tableRow == 2)
{ {
tableCol = line.split("\t").count(); // 获取第二行的列数 rows.append(line);
tableRow++;
if (tableRow == 2)
{
QStringList columnValues = line.split("\t", QString::SkipEmptyParts);
tableCol = columnValues.count();
}
} }
} }
@ -99,7 +108,7 @@ void MainWindow::loadTextFile(const QString& fileName)
showProgressDialog("Converting...", 0, tableRow); showProgressDialog("Converting...", 0, tableRow);
// 将数据显示在表格中 // 将数据显示在表格中
QTableWidget* tableWidget = ui->tableWidget; QTableWidget* tableWidget = ui->tableWidget;
// 设置表格的行数和列数 // 设置表格的行数和列数
tableWidget->setRowCount(tableRow); tableWidget->setRowCount(tableRow);
@ -107,7 +116,6 @@ void MainWindow::loadTextFile(const QString& fileName)
tableWidget->blockSignals(true); // 阻止信号发射 tableWidget->blockSignals(true); // 阻止信号发射
QStringList rows = textData.split("\n");
for (int i = 0; i < tableRow; ++i) for (int i = 0; i < tableRow; ++i)
{ {
QStringList row = rows.at(i).split("\t"); QStringList row = rows.at(i).split("\t");
@ -123,6 +131,7 @@ void MainWindow::loadTextFile(const QString& fileName)
QTableWidgetItem* item = new QTableWidgetItem(row.at(j)); QTableWidgetItem* item = new QTableWidgetItem(row.at(j));
tableWidget->setItem(i, j, item); tableWidget->setItem(i, j, item);
} }
progressDialog->setValue(i); progressDialog->setValue(i);
QApplication::processEvents(); QApplication::processEvents();
if (progressDialog->wasCanceled()) if (progressDialog->wasCanceled())
@ -131,20 +140,67 @@ void MainWindow::loadTextFile(const QString& fileName)
break; break;
} }
} }
progressDialog->hide(); progressDialog->hide();
tableWidget->blockSignals(false); // 恢复信号发射 tableWidget->blockSignals(false); // 恢复信号发射
} }
void MainWindow::onOpenButtonClicked() void MainWindow::onOpenButtonClicked()
{ {
QString fileName = QFileDialog::getOpenFileName(this, "Open Text File", "", "Text Files (*.txt)"); openFilePath = QFileDialog::getOpenFileName(this, "Open Text File", "", "Text Files (*.txt)");
if (!fileName.isEmpty()) if (!openFilePath.isEmpty())
{ {
loadTextFile(fileName); loadTextFile(openFilePath);
} }
} }
void MainWindow::onSaveButtonClicked() 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 << "\r\n"; // 换行,注意使用\r\n表示换行符
}
file.close();
}
}
}
void MainWindow::onSaveAsButtonClicked()
{ {
QString fileName = QFileDialog::getSaveFileName(this, "Save Text File", "", "Text Files (*.txt)"); QString fileName = QFileDialog::getSaveFileName(this, "Save Text File", "", "Text Files (*.txt)");
if (!fileName.isEmpty()) if (!fileName.isEmpty())
@ -207,10 +263,10 @@ void MainWindow::dropEvent(QDropEvent *event)
QList<QUrl> urlList = mimeData->urls(); QList<QUrl> urlList = mimeData->urls();
if (urlList.length() == 1) if (urlList.length() == 1)
{ {
QString fileName = urlList.first().toLocalFile(); openFilePath = urlList.first().toLocalFile();
if (!fileName.isEmpty()) if (!openFilePath.isEmpty())
{ {
loadTextFile(fileName); loadTextFile(openFilePath);
} }
} }
} }

View File

@ -29,10 +29,12 @@ public:
private slots: private slots:
void onOpenButtonClicked(); void onOpenButtonClicked();
void onSaveButtonClicked(); void onSaveButtonClicked();
void onSaveAsButtonClicked();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
QProgressDialog* progressDialog; QProgressDialog* progressDialog;
QString openFilePath;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -42,6 +42,7 @@
</property> </property>
<addaction name="actionOpen"/> <addaction name="actionOpen"/>
<addaction name="actionSave"/> <addaction name="actionSave"/>
<addaction name="actionSaveAs"/>
</widget> </widget>
<addaction name="menuOpen"/> <addaction name="menuOpen"/>
</widget> </widget>
@ -53,12 +54,18 @@
</action> </action>
<action name="actionSave"> <action name="actionSave">
<property name="icon"> <property name="icon">
<iconset theme="Save"/> <iconset theme="Save">
<normaloff>.</normaloff>.</iconset>
</property> </property>
<property name="text"> <property name="text">
<string>保存</string> <string>保存</string>
</property> </property>
</action> </action>
<action name="actionSaveAs">
<property name="text">
<string>另存为...</string>
</property>
</action>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>