Qt实现右键菜单

18 篇文章 0 订阅

一、实现方法

QWidget提供了虚函数:

virtual void contextMenuEvent(QContextMenuEvent*event);

覆写该函数,即可。

二、Example

创建一个基本的mainwindow项目,
头文件:

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
		
	//重实现
    void contextMenuEvent(QContextMenuEvent* event) override;
public slots:
    void hello_world();
private:
    Ui::MainWindow *ui;
};

.cpp文件:

void MainWindow::contextMenuEvent(QContextMenuEvent*event)
{
    QMenu *menu = new QMenu(this);

    auto action_del = new QAction("del",this);
    auto action_copy = new QAction("copy",this);
    auto action_export = new QAction("export",this);

    connect(action_del,&QAction::triggered,this, &MainWindow::hello_world);
    menu->addAction(action_del);
    menu->exec(this->cursor().pos());
}

注意,是绑定QAction::triggered,绑QAction::trigger是无效的,不要搞混了。

三、效果

在界面上右键会弹出菜单,点击按钮触发槽函数hello_world();
这里我只添加了一个del的按钮。

QAction构造函数中可以提供图标,实现更好看的菜单。

在这里插入图片描述

补充

另一种方式,用QWidget::customContextMenuRequested信号,添加槽实现即可。
信号的发起者需要调用setContextMenuPolicy(Qt::ContextMenuPolicy)

例:

{
...
    ui.scrollAreaWidgetContents->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
    connect(ui.scrollAreaWidgetContents, &QLabel::customContextMenuRequested, this, &ImageWorkWindow::slotShowImgRightMenu);
}

void ImageWorkWindow::slotShowImgRightMenu(const QPoint& pos) {
    Q_UNUSED(pos);
    if (m_pRightMenu == nullptr) {
        m_pRightMenu = new ImgRightMenu(this);
    }

    m_pRightMenu->exec(cursor().pos());
}

···
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您好!对于在Qt使用TreeView控件,并添加右键菜单的需求,您可以按照以下步骤进行操作: 1. 创建TreeView控件:在Qt,您可以使用QTreeView类创建一个TreeView控件。例如,在您的窗口类添加以下代码: ```cpp QTreeView *treeView = new QTreeView(this); ``` 2. 创建右键菜单:使用QMenu类创建一个右键菜单。例如,在您的窗口类的构造函数添加以下代码: ```cpp QMenu *menu = new QMenu(this); menu->addAction("Action 1"); menu->addAction("Action 2"); // 添加更多的菜单项... ``` 3. 关联右键菜单和TreeView控件:通过在TreeView控件的contextMenuPolicy属性设置Qt::CustomContextMenu,启用自定义上下文菜单。然后,重写TreeView控件的contextMenuEvent()方法,以显示自定义菜单。在您的窗口类添加以下代码: ```cpp treeView->setContextMenuPolicy(Qt::CustomContextMenu); connect(treeView, &QTreeView::customContextMenuRequested, this, [=](const QPoint &pos) { QContextMenuEvent event(QContextMenuEvent::Mouse, pos); treeView->contextMenuEvent(&event); }); ``` 4. 在contextMenuEvent()方法显示右键菜单:重写TreeView控件的contextMenuEvent()方法,并在其显示自定义菜单。在您的窗口类添加以下代码: ```cpp void YourWindowClass::contextMenuEvent(QContextMenuEvent *event) { QPoint pos = event->pos(); QModelIndex index = treeView->indexAt(pos); if (index.isValid()) { QMenu *menu = new QMenu(this); menu->addAction("Action 1"); menu->addAction("Action 2"); // 添加更多的菜单项... menu->exec(treeView->viewport()->mapToGlobal(pos)); } } ``` 这样,您就可以在TreeView控件上右键单击,并显示自定义的右键菜单了。根据您的需求,您可以根据TreeView的选定项的位置,添加相应的操作。 希望对您有所帮助!如果有任何进一步的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

barbyQAQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值