[置顶] Qt 设置右键选择菜单

 闲话不说,看 关键 源码:

/*设置右键选择菜单*/
void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu *pMenu = new QMenu(ui->treeView);
    QAction *select=pMenu->addAction(tr("select"));
    QAction *print = pMenu->addAction(tr("print"));
     connect(select,SIGNAL(activated()),this,SLOT(select()));
    connect(print,SIGNAL(activated()),this,SLOT(print()));
    pMenu->exec(QCursor::pos());
}

需要在.h文件中申明该函数:

void contextMenuEvent(QContextMenuEvent *event);



鼠标双击事件:

void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
    {
    if (event->button() == Qt::LeftButton)
    {
    
   }
}

你可能感兴趣的:(qt,Signal)