how to make qmenu item checkable pyqt4 python

unice picture unice · Apr 29, 2012 · Viewed 13.2k times · Source

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_()

Answer

mata picture mata · Apr 29, 2012

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_()