QTableWidget如何添加右键功能?

1.首先初始化

    ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);

    //右键
    m_tabMenu = new QMenu(ui->tableWidget);
    action= new QAction("展示最近数据",this);
    m_tabMenu->addAction(action);
    connect(ui->tableWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slot_DL_RcOnTw(QPoint)));
    connect(action,SIGNAL(triggered(bool)),this,SLOT(slotActionInsert()));

2. 然后激活右键

void MainWindow::slot_DL_RcOnTw(QPoint pos)
{
    m_tabMenu->exec(QCursor::pos());
}

3.接着就可以添加功能

你可能感兴趣的:(QT,qt,mariadb,c++)