Matthias
Matthias

Reputation: 10389

PySide: Adding a toggle option/action to the menu bar

In my Python application I am using PySide to create the GUI, which also includes a menu bar. Adding "normal" actions (i.e. actions connected to a function) is working fine.

Now I need to add a toggable menu option to one of the menus in the bar. That new one should show a "checked" icon next to it when it is enabled, or nothing when disabled. So that menu option can either be set to on or off, but it should not call any action or connected function.

Is there a way to achieve this in standard PySide?

Upvotes: 2

Views: 4925

Answers (1)

mata
mata

Reputation: 69052

You can simply make an action checkable by using it's checkable property. After that, you can use it's isChecked method to query it's state, if you don't want to use signals to catch it's state changes.

Simple example:

from PySide.QtGui import *

def main():
    app = QApplication([])

    window = QMainWindow()
    bar = QMenuBar(window)
    window.setMenuBar(bar)
    m = QMenu('menu', bar)
    bar.addMenu(m)
    action = QAction('action', m, checkable=True)
    m.addAction(action)

    window.show()
    app.exec_()
    print(action.isChecked())

if __name__ == '__main__':
    main()

Upvotes: 2

Related Questions