Old rules, just start the steps
Step 1: Set the contextMenuPolicy property of the button on the UI interface and select CustomContextMenu to save
Step 2: Right-click the button to go to the slot, signal select customContextMenuRequested
Step 3: After turning to the slot, make the following code in the slot function
void MainWindow::on_pushButton_customContextMenuRequested(const QPoint &/*pos*/) // 注释掉pos
{
QMenu *cmenu = new QMenu(ui->pushButton);
QAction *action1 = new QAction(tr("删除"), this);
action1->setData(1);
cmenu->addAction(action1);
// 下面这个on_menu_click(bool)槽函数做自己想做的事
connect(action1, SIGNAL(triggered(bool)), this, SLOT(on_menu_click(bool)));
cmenu->exec(QCursor::pos());
}
Step 4: Add header file
#include <QMenu>
Step 5: Run to see the effect