Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make qmenu item checkable pyqt4 python

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_()
like image 236
unice Avatar asked Apr 29 '12 01:04

unice


People also ask

What is menus in PyQt?

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.

How do I get the qmenubar of a PyQt window?

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.

How do I add a new qmenu to a qmenu object?

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.

How to create a popup menu using PyQt API?

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.


1 Answers

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_()
like image 134
mata Avatar answered Sep 18 '22 15:09

mata