首页 > 其他 > 详细

QT5 QTreeView添加右键菜单

时间:2019-10-05 21:27:25      阅读:1076      评论:0      收藏:0      [点我收藏+]

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.效果显示

技术分享图片

QT5 QTreeView添加右键菜单

原文:https://www.cnblogs.com/JustNo/p/11625744.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!