PyQT5: how to grey out and inactivate line edit with state of the radio button

Viewed 2033

I am using PyQt5 and I would like to grey out and make line edit inactive when the radio button is toggled.

1st state (default)

(o) A

( ) B

--------------------
|///////////////////|      <- Line edit inactive and greyed out, no input allowed (default state)
--------------------


2nd state

( ) A

(o) B

--------------------
|                  |      <- Line edit active input allowed 
--------------------

Any ideas? thank you!

2 Answers

Each button emits a toggled signal whenever it's clicked and changed.

I would add a slot that connects to the lower button's toggled signal, then grays out the text box or enables it depending on the state of the button.

You can connect the QLineEdit's setDisabled function to the QRadioButton's toggled Signal:

radio_button_a.toggled.connect(line_edit.setDisabled)

This works, because the toggled signal of QRadioButton emits the current state of the radio button as bool (see Qt documentation) and the setDisabled() function takes exactly one bool (Qt documentation).

Additionally, you'll have to make sure that the initial disabled state of the LineEdit matches the initial check state of your RadioButtons. If you want to take further actions on toggling of the RadioButtons, you'll need to write an own Slot (callback function) as suggested by @GandhiGandhi.

Related