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

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->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));
@ -69,28 +72,34 @@ void MainWindow::loadTextFile(const QString& 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();
}
}
}
@ -99,7 +108,7 @@ void MainWindow::loadTextFile(const QString& fileName)
showProgressDialog("Converting...", 0, tableRow);
// 将数据显示在表格中
QTableWidget* tableWidget = ui->tableWidget;
QTableWidget* tableWidget = ui->tableWidget;
// 设置表格的行数和列数
tableWidget->setRowCount(tableRow);
@ -107,7 +116,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");
@ -123,6 +131,7 @@ void MainWindow::loadTextFile(const QString& fileName)
QTableWidgetItem* item = new QTableWidgetItem(row.at(j));
tableWidget->setItem(i, j, item);
}
progressDialog->setValue(i);
QApplication::processEvents();
if (progressDialog->wasCanceled())
@ -131,20 +140,67 @@ void MainWindow::loadTextFile(const QString& fileName)
break;
}
}
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 << "\r\n"; // 换行,注意使用\r\n表示换行符
}
file.close();
}
}
}
void MainWindow::onSaveAsButtonClicked()
{
QString fileName = QFileDialog::getSaveFileName(this, "Save Text File", "", "Text Files (*.txt)");
if (!fileName.isEmpty())
@ -207,10 +263,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);
}
}
}

View File

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

View File

@ -42,6 +42,7 @@
</property>
<addaction name="actionOpen"/>
<addaction name="actionSave"/>
<addaction name="actionSaveAs"/>
</widget>
<addaction name="menuOpen"/>
</widget>
@ -53,12 +54,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/>