0

Is there a way for a QToolButton to display its associated QMenu on top of it instead of below?

I have seen this answer which pleased me but it's in Python and I don't really know how to port it properly.

I also took a look at the source code for QMenu but it's quite overwhelming so I don't really know how to tackle this issue.

There also is a little arrow on the button showing that it'll pop down. Here is the situation

This is the very bottom of my window so I'd like it to pop up in case it becomes too big to fit.

Telokis
  • 3,399
  • 14
  • 36

1 Answers1

1

You could do it using an event filter:

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;
}

To remove the little arrow, add this to your stylesheet:

QToolButton::menu-indicator{
    image: none;
}
IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
  • Thanks! Alright, I didn't know this thing, I'll give it a shot. While you're here, is there a way to make the little arrow point upwards on the button? And if it's not possible, can I just remove it completely? – Telokis Nov 13 '17 at 16:07
  • 1
    @Telokis Yes, added the removal of the arrow :-) – IAmInPLS Nov 13 '17 at 16:20
  • 1
    Thank you very much. That's enough for me! :) – Telokis Nov 13 '17 at 16:26