Reputation: 108
I have to align 'Menu5' to the right side of MenuBar. Is that possible in Python? (PyQt4)
I found information on how to do this in C there
Aligning QMenuBar items (add some on left and some on right side)
But I don't know how I can do this in Python.
My code:
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
grid = QtGui.QGridLayout()
panel = QtGui.QWidget()
panel.setLayout(grid)
self.setCentralWidget(panel)
menubar1 = self.menuBar()
menubar1.addMenu('&Menu1')
menubar1.addMenu('&Menu2')
menubar1.addMenu('&Menu3')
menubar1.addMenu('&Menu4')
menubar1.addMenu('&Menu5')
self.setLayout(grid)
self.move(300, 150)
self.setWindowTitle('TestApp')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Please help.
Upvotes: 1
Views: 2855
Reputation: 36
QMenuBar has setCornerWidget function which allows you to do that.
menubar1 = self.menuBar()
menubar1.addMenu('&Menu1')
menubar1.addMenu('&Menu2')
menubar1.addMenu('&Menu3')
menubar1.addMenu('&Menu4')
self.menuBr= QtGui.QMenuBar(menubar1)
menubar1.setCornerWidget(self.menuBr, QtCore.Qt.TopRightCorner)
self.menu5 = QtGui.QMenu(self.menuBr)
self.menu5.setTitle("Menu5")
self.menuBr.addAction(self.menu5.menuAction())
Upvotes: 2