QToolButton: change menu position

When using a menu with a menu, QToolButton displayed directly below the button. Is there a way to display the menu left / right of the button?

+4
source share
2 answers

The position is hardcoded in the void QToolButtonPrivate::popupTimerDone() function void QToolButtonPrivate::popupTimerDone() in the [Qt install] /src/gui/widgets/qtoolbutton.cpp directory. It seems pretty hard to override this unless you implement your own popup menu from scratch.

+3
source

I know this question was answered some time ago, but I wanted to add a new answer to this question, since the accepted answer is no longer valid. It is actually quite easy to change the position of the menu to QToolButton. You must subclass QMenu and override the event function. When there is a show event, just reposition the menu.

Here is a simple example of using PySide:

 from PySide import QtCore, QtGui class MyMenu(QtGui.QMenu): def event(self,event): if event.type() == QtCore.QEvent.Show: self.move(self.parent().mapToGlobal(QtCore.QPoint(0,0))-QtCore.QPoint(0,self.height())) return super(MyMenu,self).event(event) if __name__ == "__main__": app = QtGui.QApplication([]) w = QtGui.QWidget() w.setGeometry(100,100,500,500) tb = QtGui.QToolButton(w) tb.setText("HELLO") tb.setGeometry(70,70,40,30) m = MyMenu("Menu",tb) m.addAction("Exit") tb.setMenu(m) w.show() app.exec_() 
+1
source

Source: https://habr.com/ru/post/1385108/


All Articles