From 4bd4ee655d7db0ac59fa889c448e23e4ab70df1c Mon Sep 17 00:00:00 2001 From: Crimson Date: Sun, 13 Aug 2023 23:44:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B7=BB=E5=8A=A0=E5=88=97?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mainwindow.cpp | 74 +++++++++++++++++++++++++++++++++++--------------- mainwindow.h | 5 +++- 2 files changed, 56 insertions(+), 23 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index de3fc38..5f3d0b5 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -49,11 +49,17 @@ MainWindow::MainWindow(QWidget *parent) textToSqliteAction->setIcon(convertIcon); sqliteToTextAction->setIcon(convertIcon); - contextMenu = new QMenu(this); - QAction* addRowAboveAction = contextMenu->addAction(tr("向上添加一行")); - QAction* addRowBelowAction = contextMenu->addAction(tr("向下添加一行")); + firstColMenu = new QMenu(this); + QAction* addRowAboveAction = firstColMenu->addAction(tr("↑ 向上添加一行")); + QAction* addRowBelowAction = firstColMenu->addAction(tr("向下添加一行 ↓")); connect(addRowAboveAction, &QAction::triggered, this, &MainWindow::onAddRowAboveActionTriggered); connect(addRowBelowAction, &QAction::triggered, this, &MainWindow::onAddRowBelowActionTriggered); + + firstRowMenu = new QMenu(this); + QAction* addColAboveAction = firstRowMenu->addAction(tr("← 向左添加一列")); + QAction* addColBelowAction = firstRowMenu->addAction(tr("向右添加一列 →")); + connect(addColAboveAction, &QAction::triggered, this, &MainWindow::onAddColAboveActionTriggered); + connect(addColBelowAction, &QAction::triggered, this, &MainWindow::onAddColBelowActionTriggered); } @@ -66,16 +72,21 @@ 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) - { - int currentRow = ui->tableWidget->rowAt(cellPos.y()); + if (currentColumn == -1 && currentRow == -1) { + // 没有选中行也没有选中列,不显示菜单 + return; + } - if (currentRow >= 0) - { - ui->tableWidget->selectRow(currentRow); // Select the clicked row - contextMenu->popup(event->globalPos()); - } + 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()); } } @@ -94,13 +105,6 @@ void MainWindow::onAddRowAboveActionTriggered() 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() @@ -117,12 +121,38 @@ void MainWindow::onAddRowBelowActionTriggered() int newRow = tableWidget->rowCount(); tableWidget->insertRow(newRow); } +} - // 填充插入的行为空项 - for (int column = 0; column < tableWidget->columnCount(); ++column) +void MainWindow::onAddColAboveActionTriggered() +{ + QTableWidget* tableWidget = ui->tableWidget; + int currentColumn = tableWidget->currentColumn(); + if (currentColumn >= 0) { - QTableWidgetItem* emptyItem = new QTableWidgetItem(""); - tableWidget->setItem(currentRow + 1, column, emptyItem); + 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); } } diff --git a/mainwindow.h b/mainwindow.h index 23873a2..44ddbba 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -33,12 +33,15 @@ private slots: void onSaveAsButtonClicked(); void onAddRowAboveActionTriggered(); void onAddRowBelowActionTriggered(); + void onAddColAboveActionTriggered(); + void onAddColBelowActionTriggered(); private: Ui::MainWindow *ui; QProgressDialog* progressDialog; QString openFilePath; - QMenu* contextMenu; // 右键菜单 + QMenu* firstColMenu; // 右键菜单 + QMenu* firstRowMenu; // 右键菜单 }; #endif // MAINWINDOW_H