增加右键新增行功能

This commit is contained in:
阿甘 2023-08-13 20:32:19 +08:00
parent f55de36c25
commit 5744d80e69
2 changed files with 86 additions and 1 deletions

View File

@ -12,6 +12,7 @@
#include <QInputDialog>
#include <QSqlRecord>
#include <QtDebug>
#include <QContextMenuEvent>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
@ -47,6 +48,14 @@ MainWindow::MainWindow(QWidget *parent)
QIcon convertIcon = QApplication::style()->standardIcon(QStyle::SP_ArrowRight);
textToSqliteAction->setIcon(convertIcon);
sqliteToTextAction->setIcon(convertIcon);
ui->tableWidget->installEventFilter(this);
contextMenu = new QMenu(this);
QAction* addRowAboveAction = contextMenu->addAction(tr("在上方添加行"));
QAction* addRowBelowAction = contextMenu->addAction(tr("在下方添加行"));
connect(addRowAboveAction, &QAction::triggered, this, &MainWindow::onAddRowAboveActionTriggered);
connect(addRowBelowAction, &QAction::triggered, this, &MainWindow::onAddRowBelowActionTriggered);
}
@ -55,6 +64,78 @@ MainWindow::~MainWindow()
delete ui;
}
bool MainWindow::eventFilter(QObject* obj, QEvent* event)
{
if (obj == ui->tableWidget && event->type() == QEvent::ContextMenu)
{
QContextMenuEvent* contextMenuEvent = static_cast<QContextMenuEvent*>(event);
contextMenuEvent->accept();
contextMenu->exec(contextMenuEvent->globalPos());
return true; // 事件已处理
}
return QMainWindow::eventFilter(obj, event);
}
void MainWindow::contextMenuEvent(QContextMenuEvent* event)
{
QPoint cellPos = ui->tableWidget->mapFromGlobal(event->globalPos());
int currentRow = ui->tableWidget->rowAt(cellPos.y());
int currentColumn = ui->tableWidget->columnAt(cellPos.x());
if (currentRow >= 0 && currentColumn == 0)
{
contextMenu->exec(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;
}
// 填充插入的行为空项
for (int column = 0; column < tableWidget->columnCount(); ++column)
{
QTableWidgetItem* emptyItem = new QTableWidgetItem("");
tableWidget->setItem(currentRow, column, emptyItem);
}
}
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);
}
// 填充插入的行为空项
for (int column = 0; column < tableWidget->columnCount(); ++column)
{
QTableWidgetItem* emptyItem = new QTableWidgetItem("");
tableWidget->setItem(currentRow + 1, column, emptyItem);
}
}
void MainWindow::showProgressDialog(const QString& labelText, int minimum, int maximum)
{
progressDialog = new QProgressDialog(labelText, "Cancel", minimum, maximum, this);
@ -513,4 +594,3 @@ void MainWindow::convertSqliteToText()
QMessageBox::information(this, tr("Conversion Complete"), tr("SQLite to Text conversion completed successfully."));
}

View File

@ -25,16 +25,21 @@ public:
void convertTextToSqlite();
void convertSqliteToText();
void showProgressDialog(const QString& labelText, int minimum, int maximum);
bool eventFilter(QObject* obj, QEvent* event);
void contextMenuEvent(QContextMenuEvent* event);
private slots:
void onOpenButtonClicked();
void onSaveButtonClicked();
void onSaveAsButtonClicked();
void onAddRowAboveActionTriggered();
void onAddRowBelowActionTriggered();
private:
Ui::MainWindow *ui;
QProgressDialog* progressDialog;
QString openFilePath;
QMenu* contextMenu; // 右键菜单
};
#endif // MAINWINDOW_H