QTreeView右键菜单

****第一步 把QTreeView指针的customContextMenuRequested信号连接到槽函数, 设置setContextMenuPolicy(Qt::CustomContextMenu)(此函数为QTreeView成员)**** 

bool bC2 = connect(m_geodeView, SIGNAL(customContextMenuRequested(const QPoint&)), m_nodeOperation, SLOT(slot_customContextMenuRequested(const QPoint&)));

****第二部 槽函数****

void NodeOper::slot_customContextMenuRequested(const QPoint & pos)
{
    QTreeView* treeView = qobject_cast*>(sender());  // sender()是QObejct的函数,用来获取连接时信号的发射指针

    if (!treeView)
    {
        return;
    }

    QModelIndex index = treeView->indexAt(pos);

    if (index.isValid())
    {
        QMenu menu;
        menu.addAction("Show");
        menu.addAction("delete");

        menu.exec(QCursor::pos());  //QCursor::pos()让menu的位置在鼠标点击的的位置, 
    }

}

你可能感兴趣的:(Qt)