0

Is there a way to make QAction stay down after it is clicked. Ideally it could toggle between two states: On (down) and Off (up)?

alphanumeric
  • 17,967
  • 64
  • 244
  • 392
  • http://stackoverflow.com/questions/13506201/how-to-add-check-uncheck-qaction-in-the-context-menu – furas Jan 11 '17 at 17:39
  • 1
    I'm honestly not having a dig here, but are you on a mission to write all your pyqt code without ever reading any documentation? I'm just curious why you would apparently spend an hour on this, when it would only take you a few minutes at most to consult the [Qt docs for QAction](https://doc.qt.io/qt-4.8/qaction.html#checked-prop) and find all the answers you need. As an added bonus, in this particular case you might also have discovered a better signal to use as well (i.e. `toggled` *vs.* `triggered`). – ekhumoro Jan 11 '17 at 19:41
  • Thanks for `toggled` vs `triggered` signal clue! I am not on a mission. I just like Stackoverflow. – alphanumeric Jan 12 '17 at 04:52
  • You should like the docs too :) and not expect others to do your (doc-reading) work for you... – Oliver Jan 12 '17 at 06:51

2 Answers2

1
action = QtGui.QAction(QtGui.QIcon("myicon.png"),"Action Name", None)
action.setCheckable(True)
action.setStatusTip("Tooltip") 
action.setShortcut("Ctrl+D")  
action.triggered.connect(actionTrigged)
alphanumeric
  • 17,967
  • 64
  • 244
  • 392
1

What you are looking for is a toggle button. This is implemented in Qt via the checkable property: if an action is checkable, then when the action is in a button the button is a toggle button; when the action is in a menu item you see a checkmark; etc.

Oliver
  • 27,510
  • 9
  • 72
  • 103