更新加载进度条功能

This commit is contained in:
一二三 2023-07-10 22:57:48 +08:00
parent c2e46b264a
commit d59b08a145
2 changed files with 44 additions and 19 deletions

View File

@ -19,7 +19,7 @@ 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);
@ -33,14 +33,11 @@ MainWindow::MainWindow(QWidget *parent)
ui->actionOpen->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N));
ui->actionSave->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
// 添加 "Convert" 菜单
QMenu* convertMenu = menuBar()->addMenu(tr("转换(&C)"));
// 添加 "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 转 txt"));
connect(sqliteToTextAction, &QAction::triggered, this, &MainWindow::convertSqliteToText);
@ -49,11 +46,23 @@ MainWindow::MainWindow(QWidget *parent)
sqliteToTextAction->setIcon(convertIcon);
}
MainWindow::~MainWindow()
{
delete ui;
}
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);
@ -87,13 +96,7 @@ void MainWindow::loadTextFile(const QString& fileName)
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;
@ -121,14 +124,13 @@ void MainWindow::loadTextFile(const QString& fileName)
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); // 恢复信号发射
}
@ -255,7 +257,6 @@ void MainWindow::convertTextToSqlite()
columnNames = processedLine.split('\t', Qt::SkipEmptyParts);
}
//QTextStream textStream2(&textFile);
QList<QStringList> data;
textStream.seek(0);
// 读取数据行
@ -275,6 +276,8 @@ void MainWindow::convertTextToSqlite()
textFile.close();
showProgressDialog("Converting...", 0, data.size());
// 打开SQLite数据库
QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName(sqliteFilePath);
@ -311,6 +314,7 @@ void MainWindow::convertTextToSqlite()
insertQuery.chop(2); // 移除最后的逗号和空格
insertQuery += ");";
int progress = 0;
for (const QStringList& row : data)
{
if (row.size() != columnNames.size())
@ -332,8 +336,17 @@ void MainWindow::convertTextToSqlite()
database.close();
return;
}
++progress;
progressDialog->setValue(progress);
QApplication::processEvents();
if (progressDialog->wasCanceled())
{
break;
}
}
progressDialog->hide();
database.commit();
database.close();
@ -389,6 +402,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))
@ -408,6 +423,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();
@ -419,10 +435,20 @@ void MainWindow::convertSqliteToText()
textStream << "\t";
}
textStream << "\r\n";
++progress;
progressDialog->setValue(progress);
QApplication::processEvents();
if (progressDialog->wasCanceled())
{
break;
}
}
progressDialog->hide();
textFile.close();
database.close();
QMessageBox::information(this, tr("Conversion Complete"), tr("SQLite to Text conversion completed successfully."));
}

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,6 +24,7 @@ public:
void dropEvent(QDropEvent *event);
void convertTextToSqlite();
void convertSqliteToText();
void showProgressDialog(const QString& labelText, int minimum, int maximum);
private slots:
void onOpenButtonClicked();
@ -32,8 +32,7 @@ private slots:
private:
Ui::MainWindow *ui;
QProgressDialog* progressDialog; // 添加进度对话框指针
QMenu* menuFile;
QAction* actionFile;
QProgressDialog* progressDialog;
};
#endif // MAINWINDOW_H