增加添加列功能

This commit is contained in:
阿甘 2023-08-13 23:44:42 +08:00
parent dd9f9408c1
commit 4bd4ee655d
2 changed files with 56 additions and 23 deletions

View File

@ -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);
}
}

View File

@ -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