Skip to content

Instantly share code, notes, and snippets.

@510908220
Last active October 14, 2015 15:26
Show Gist options
  • Save 510908220/71f3a439e017caa8b824 to your computer and use it in GitHub Desktop.
Save 510908220/71f3a439e017caa8b824 to your computer and use it in GitHub Desktop.
QListWidget的item上实现右键菜单
    void ListWidget::contextMenuEvent ( QContextMenuEvent * event )
    {
        QMenu* popMenu = new QMenu(this);
        popMenu->addAction(new QAction("添加", this));
        popMenu->addAction(new QAction("删除", this));
        if(this->itemAt(mapFromGlobal(QCursor::pos())) != NULL) //如果有item则添加"修改"菜单 [1]*
        {
            popMenu->addAction(new QAction("修改", this));
        }
        
        popMenu->exec(QCursor::pos()); // 菜单出现的位置为当前鼠标的位置
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment