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添加右键菜单

相关文章:

  • 2021-10-15
  • 2021-12-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-25
  • 2021-10-26
  • 2021-12-06
  • 2021-10-19
  • 2022-01-03
相关资源
相似解决方案