zaxvo
zaxvo

Reputation: 186

Turn QPushButton on and off

I have an app in PyQt with a few buttons to shift between modes of the application.

I have the clicked() signals of the buttons linked to the appropriate methods. My problem is that there are other ways to change modes (for instance, loading settings will automatically move the user to their default mode), and I'm using the QPushButtons as indicators of the "active mode". This was previously accomplished by having two icons for each button, one for the button being off and the other for the button being on. It's all been designed in QtDesigner, so clicking on one button turns it on (and turns the other buttons off) and changes the icons appropriately. And when the mouse is released, that button stays on.

The button stays on until another button is pressed.

I'm trying to figure out how I can change a button from "on" to "off" without the user actually pressing the button, so I can change modes appropriately.

The Qt docs make reference to the property I'm looking for but I can't find any more details than the fact that these states exist:

The most important modes or states are:

  1. Available or not (grayed out, disabled).

  2. Standard push button, toggling push button or menu button.

  3. On or off (only for toggling push buttons).

  4. Default or normal. The default button in a dialog can generally be "clicked" using the Enter or Return key.

  5. Auto-repeat or not.

  6. Pressed down or not.

(http://qt-project.org/doc/qt-5/QPushButton.html)

To be more specific, I'm looking for a way to see the state of a QPushButton; to see whether it is "On" or "Off", and I'm looking for a way to change that state.

EDIT: I found the appropriate method QPushButton.isOn() but the problem is that it's in Qt3. (I'm using Qt5, where this method no longer exists). Clearly it's obsolete now, would anyone happen to know what replaced it? http://doc.qt.digia.com/3.2/qpushbutton.html#isOn

Upvotes: 2

Views: 10169

Answers (1)

NoDataDumpNoContribution
NoDataDumpNoContribution

Reputation: 10860

QPushButton inherits from QAbstractButton and and therefore has the following methods: isChecked, setChecked and isCheckable, setCheckable. This way you can convert the button into a toggle button and ask/set the state. There is also setAutoRepeat and autoRepeat which controls the auto repeat programmatically.

Especially instead of isOn use isChecked.

Furthermore it also inherits from QWidget which has methods isEnabled and setEnabled. With this you can activate/de-active the button which is shown by graying out the button as well as by prohibiting clicks on the button.

Basically just study the documentation for QAbstractButton and QWidget to see how you can programmatically interact with a QPushButton to enable/disable it.

Upvotes: 3

Related Questions