QTabWidget 选项卡上下文菜单

发布于 2024-08-06 00:03:44 字数 64 浏览 7 评论 0 原文

每当单击选项卡时,我需要显示上下文菜单,并且它需要对该特定选项卡做出反应。有没有办法在不子类化的情况下做到这一点?

I need to display a context menu whenever a tab is clicked on and it needs to react to that specific tab. Is there any way to do this without subclassing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森林散布 2024-08-13 00:03:44

简单的方法,但可能不完全是您需要的:

  1. 连接到 QTabWidget 的“currentChanged”信号
  2. 在连接到信号的插槽中,创建一个 QMenu 并根据需要填充它
  3. 最后,在连接到信号的插槽中,调用 QMenu::exec( QCursor::pos() )

这将在选项卡更改(不一定单击)时调用一个函数,并且
在当前鼠标位置生成一个菜单。

复杂的方式,完全符合您的描述:

  1. 调用 QObject::installEventFilter 在你的 QTabWidget 上,这样你的 QTabWidget 上的所有事件都被重定向到你自己的对象。
  2. 在您自己的对象中,重新实现 QObject::customEvent 并处理所有 QMouseEvent事件。
  3. 根据需要填充 QMenu 并在您正在处理的 QMouseEvent 位置调用 QMenu::exec。

Easy way, but possibly not precisely what you need:

  1. Connect to the 'currentChanged' signal of your QTabWidget
  2. In the slot which is connected to the signal, create a QMenu and populate it as needed
  3. Finally, in the slot which is connected to the signal, call QMenu::exec( QCursor::pos() )

This will get a function called whenever the tab is changed (not necessarily clicked) and
spawn a menu at the current mouse position.

Complicated way, which exactly does what you describe:

  1. Call QObject::installEventFilter on your QTabWidget, so that all the events on your QTabWidget are redirected to your own object.
  2. In your own object, reimplement QObject::customEvent and handle all QMouseEvent events.
  3. Populate a QMenu as needed and call QMenu::exec at the position of the QMouseEvent you're handling.
爱人如己 2024-08-13 00:03:44

创建 QMenu:

m_menu = new QMenu;

将您的操作添加到菜单。

创建一个在选项卡栏上请求上下文菜单时调用的槽:

connect(m_tabWidget->tabBar(), &QTabBar::tabBarClicked, this, &MyClass::on_contextMenuRequested);

在槽中显示菜单。槽的定义:

void MyClass::on_contextMenuRequested(int tabIndex)
{
    m_menu->popup(QCursor::pos());
}

如果您需要在另一个函数中当前选项卡的索引,请使用以下内容:

m_tabWidget->tabBar()->currentIndex()

create a QMenu:

m_menu = new QMenu;

add your actions to menu.

Create a slot to be called when context menu requested on tab bar:

connect(m_tabWidget->tabBar(), &QTabBar::tabBarClicked, this, &MyClass::on_contextMenuRequested);

In the slot, show the menu. Definition of slot:

void MyClass::on_contextMenuRequested(int tabIndex)
{
    m_menu->popup(QCursor::pos());
}

If you need index of current tab in another function, use following:

m_tabWidget->tabBar()->currentIndex()
苏佲洛 2024-08-13 00:03:44

根据 @Petrzio Berkerle 的评论,在 https://www.qtcentre.org/threads/16703-QTabBar-Context-menu-on-tab?p=84057#post84057 对我来说效果很好。 (实际上,这是唯一有效的。)

那里的帖子中的代码(通过“spirit”):

    ...
    m_tabBar = new QTabBar();
    m_tabBar->addTab(tr("OK"));
    m_tabBar->addTab(tr("NO"));
    m_tabBar->addTab(tr("IGNORE"));
    m_tabBar->setContextMenuPolicy(Qt::CustomContextMenu);
     
    connect(m_tabBar, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenu(const QPoint &)));
    ...
     
    void Test::showContextMenu(const QPoint &point)
    {
        if (point.isNull())
            return;
     
        int tabIndex = m_tabBar->tabAt(point);
        QMenu menu(this);
        if (!tabIndex)
            menu.addAction(tr("OK"));
        else if (tabIndex == 1)
            menu.addAction(tr("NO"));
        else if (tabIndex == 2)
            menu.addAction(tr("IGNORE"));
     
        menu.exec(m_tabBar->mapToGlobal(point));
    }

As per the comment by @Petrzio Berkerle, the solution found at https://www.qtcentre.org/threads/16703-QTabBar-Context-menu-on-tab?p=84057#post84057 worked very well for me. (Actually, it was the only one that worked at all.)

The code from the post there (by "spirit"):

    ...
    m_tabBar = new QTabBar();
    m_tabBar->addTab(tr("OK"));
    m_tabBar->addTab(tr("NO"));
    m_tabBar->addTab(tr("IGNORE"));
    m_tabBar->setContextMenuPolicy(Qt::CustomContextMenu);
     
    connect(m_tabBar, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenu(const QPoint &)));
    ...
     
    void Test::showContextMenu(const QPoint &point)
    {
        if (point.isNull())
            return;
     
        int tabIndex = m_tabBar->tabAt(point);
        QMenu menu(this);
        if (!tabIndex)
            menu.addAction(tr("OK"));
        else if (tabIndex == 1)
            menu.addAction(tr("NO"));
        else if (tabIndex == 2)
            menu.addAction(tr("IGNORE"));
     
        menu.exec(m_tabBar->mapToGlobal(point));
    }
浴红衣 2024-08-13 00:03:44

我认为您需要创建自己的类,该类继承自 QTabWidget 并覆盖 MousePressEvent(QMouseEvent) 受保护函数,您可以在其中右键单击时创建上下文菜单。

I think you need to create your own class that inherits from QTabWidget and override the MousePressEvent(QMouseEvent) protected function in which you can create your context menu on right click.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文