How can i make my qmenu checkable?
from PyQt4 import QtGui
app = QtGui.QApplication([])
menu = QtGui.QMenu()
menu.addAction('50%')
menu.addAction('100%')
menu.addAction('200%')
menu.addAction('400%')
menu.show()
app.exec_()
Menus are pull-down lists of menu options that you can trigger by clicking them or by hitting a keyboard shortcut. There are at least three ways for adding menus to a menu bar object in PyQt: QMenuBar.addMenu (menu) appends a QMenu object ( menu) to a menu bar object. It returns the action associated with this menu.
In a PyQt main window–style application, QMainWindow provides an empty QMenuBar object by default. To get access to this menu bar, you need to call .menuBar () on your QMainWindow object. This method will return an empty menu bar.
QMenuBar.addMenu (title) creates and appends a new QMenu object with the string ( title) as its title to a menu bar. The menu bar takes the ownership of the menu and the method returns the new QMenu object. QMenuBar.addMenu (icon, title) creates and appends a new QMenu object with an icon and a title to a menu bar object.
To create a popup menu, PyQt API provides createPopupMenu() function. menuBar() function returns main window’s QMenuBar object. addMenu() function lets addition of menu to the bar. In turn, actions are added in the menu by addAction() method. Following table lists some of the important methods used in designing a menu system.
like this:
from PyQt4 import QtGui
app = QtGui.QApplication([])
w = QtGui.QMainWindow()
menu = QtGui.QMenu("menu", w)
menu.addAction(QtGui.QAction('50%', menu, checkable=True))
menu.addAction(QtGui.QAction('100%', menu, checkable=True))
menu.addAction(QtGui.QAction('200%', menu, checkable=True))
menu.addAction(QtGui.QAction('300%', menu, checkable=True))
menu.addAction(QtGui.QAction('400%', menu, checkable=True))
w.menuBar().addMenu(menu)
w.show()
app.exec_()
or witht radio buttons:
from PyQt4 import QtGui
app = QtGui.QApplication([])
w = QtGui.QMainWindow()
menu = QtGui.QMenu("menu", w)
ag = QtGui.QActionGroup(w, exclusive=True)
a = ag.addAction(QtGui.QAction('50%', w, checkable=True))
menu.addAction(a)
a = ag.addAction(QtGui.QAction('100%', w, checkable=True))
menu.addAction(a)
a = ag.addAction(QtGui.QAction('200%', w, checkable=True))
menu.addAction(a)
a = ag.addAction(QtGui.QAction('300%', w, checkable=True))
menu.addAction(a)
a = ag.addAction(QtGui.QAction('400%', w, checkable=True))
menu.addAction(a)
w.menuBar().addMenu(menu)
w.show()
app.exec_()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With