c++ - 在 Qt 5.9 中使 QToolButton 显示菜单在顶部

标签 c++ qt user-interface qt5.9

QToolButton 有办法吗?显示其关联的 QMenu在它上面而不是下面?

我看过this回答让我很高兴,但它是用 Python 编写的,我真的不知道如何正确移植它。

我还查看了 source code for QMenu但这太让人不知所措了,所以我真的不知道如何解决这个问题。

按钮上还有一个小箭头,表示它会弹出。 Here is the situation

这是我窗口的最底部,所以我希望它弹出,以防它变得太大而放不下。

最佳答案

您可以使用事件过滤器来做到这一点:

QMenu* yourMenu;
yourButton->setMenu(yourMenu);
yourMenu->installEventFilter(this);

bool yourClass::eventFilter(QObject * obj, QEvent *event)
{
    if (event->type() == QEvent::Show && obj == yourButton->menu())
    {
        QPoint pos = /*the position expected*/;
        yourButton->menu()->move(pos);
        return true;
    }
    return false;
}

要删除小箭头,请将其添加到您的样式表中:

QToolButton::menu-indicator{
    image: none;
}

关于c++ - 在 Qt 5.9 中使 QToolButton 显示菜单在顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47267652/

相关文章:

c++:尝试使用const成员构造类

c++ - 平衡二叉搜索树实现

qt - 杀死停止并恢复的 QProcess

c++ - Qt - GUI 数据库编程

qt - 使用 Qt Creator(与 Qwt),非常基本的东西

c# - Windows 中的自动完成文本框和 "Hide Pointer While Typing"

c++ - 如何将 PreShutdown 事件添加到 ATL 服务?

GWT CRUD 图形用户界面模型

C# Gui 设置 control.Enabled 是否会错误触发 OnClick 事件?

c++ - 促进 ASIO 异常传播