1. 程式人生 > >QTreeWidget新增右鍵選單

QTreeWidget新增右鍵選單

1.設定QtreeWidget模式

ui.treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);

2.建立menu

m_menu = new QMenu(this);
m_action1 = new QAction(tr("計劃1"), this);
m_action2 = new QAction(tr("計劃2"), this);
m_menu->addAction(m_action1);
m_menu->addAction(m_action2);
connect(m_action1, &QAction::triggered, this, &MainWindow::slot_checkPlan1);
connect(m_action2, &QAction::triggered, this, &MainWindow::slot_checkPlan2);

3.顯示menu

QTreeWidgetItem* curItem = ui.treeWidget->itemAt(pos);  //獲取當前被點選的節點
if (curItem == NULL)return;           //這種情況是右鍵的位置不在treeItem的範圍內,即在空白位置右擊

m_sceneMenu->exec(QCursor::pos());