梦想是去放羊|

SheepHuan

园龄:6年5个月粉丝:21关注:5

QT5 QTreeView添加右键菜单

C++ QT5学习——QTreeView控件创建右键菜单

QTreeView是QWidget的子类,我们再改写QTreeView类的时候,注意的是继承关系。

1.TreeView.h

class TreeView : public QTreeView//记得加public 不然是私有继承
{
Q_OBJECT //使用信号与槽所必需的
public:
TreeView();
public slots:
void slotCustomContextMenu(const QPoint &point);//创建右键菜单的槽函数
};

切入正题。

对于QTreeView实现右键菜单是通过信号与槽实现的。

我们在点击右键的时候会发生customContextMenuRequested(const QPoint &)信号。我们根据这个信号创建菜单就行了

2.TreeView.cpp

TreeView::TreeView() :QTreeView() //构造函数
{
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this,SIGNAL(customContextMenuRequested(const QPoint &)),this, SLOT(slotCustomContextMenu(const QPoint &)));
}
void TreeView::slotCustomContextMenu(const QPoint &point) //槽函数定义
{
QMenu *menu = new QMenu(this);
QAction *a1=new QAction(tr("上传"));
menu->addAction(a1);
QAction *a2=new QAction(tr("移动"));
menu->addAction(a2);
QAction *a3=new QAction(tr("复制"));
menu->addAction(a3);
QAction *a4=new QAction(tr("删除"));
menu->addAction(a4);
menu->exec(this->mapToGlobal(point));
}

这样就实现了右键的菜单显示

3.效果显示

捕获.PNG

本文作者:SheepHuan

本文链接:https://www.cnblogs.com/sheephuan/p/11625744.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   SheepHuan  阅读(3928)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起