How to disable a particular standard button in pyqt

Viewed 3524

I am working on python application where I have created the dialog which includes several standard buttons like: Reset, OK, Cancel, Apply. here is the relevant code for the standard button,

self.buttonBox = QtGui.QDialogButtonBox(ROI)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Reset|QtGui.QDialogButtonBox.Apply|QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName( ("buttonBox"))

My question is how to disable only Apply button Even I tried the following code

QtGui.QDialogButtonBox.Apply.setEnabled(False)

but getting error

AttributeError: 'StandardButton' object has no attribute 'setEnabled'

So how do I disable particular button in set of standard button

2 Answers

@Omkar's comment as answer:

You need to find the specific object and then call its setEnabled() method with False as parameter.

For your QDialogButtonBox you can get to the specific button via

btn_apply = self.buttonBox.button(QtGui.QDialogButtonBox.Apply)

and then disable using

btn_apply.setEnabled(False)

Alternatively this can be done in one step:

self.buttonBox.button(QtGui.QDialogButtonBox.Apply).setEnabled(False)

See https://doc.qt.io/qt-5/qdialogbuttonbox.html#button for a reference on the button "retrieval" and https://doc.qt.io/qt-5/qwidget.html#enabled-prop for the disabling.

Related