zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    反悔贪心 学习笔记
    「CF901C」Bipartite Segments 题解
    「HEOI2015」公约数数列 题解
    拓展欧拉定理
    莫比乌斯反演题目选做
    八月水题录
    chapt15、使用虚拟内存
    chapt16、线程堆栈
    chapt14、探索虚拟内存
    get files version by vbs
  • 原文地址:https://www.cnblogs.com/JustNo/p/11625744.html
Copyright © 2011-2022 走看看