Eleftheria
Eleftheria

Reputation: 143

How to connect QChekBox state to QComboBox state?

I want to enable a combobox - which is disabled from the properties editor in Qt Designer - but, only if the user checks the checkbox. I wrote the following, but it is not working. It is put inside the __init__ method of my mainclass. Could you please help me to understand why?

if self.dlg.checkBox.isChecked():
    self.dlg.cmbvectorLayer6.setEnabled(True)

EDIT:

I now have the following in the __init__ method of my main class:

self.dlg.checkBox.stateChanged[int].connect(self.enablecombo)

with enablecombo being:

def enablecombo(self):
    self.dlg.cmbvectorLayer6.setEnabled(True)

and it works fine in order to activate the comboboxes. But I am not sure how to do the equivalent in order to disactivate the comboboxes when the checkbox is unchecked...

Upvotes: 2

Views: 395

Answers (3)

ekhumoro
ekhumoro

Reputation: 120768

The QCheckBox class inherits QAbstractButton, so you can use the toggled signal to do what you want:

    self.dlg.checkBox.toggled.connect(self.enablecombo)
    ...

def enablecombo(self, checked):
    self.dlg.cmbvectorLayer6.setEnabled(checked)

Or connect to the combo-box directly:

    self.dlg.checkBox.toggled.connect(self.dlg.cmbvectorLayer6.setEnabled)

(You can also set up these kinds of direct connections in Qt Designer, by using the Signals and Slots Editing Mode)

Upvotes: 2

user5509884
user5509884

Reputation: 36

self.dlg.checkBox.stateChanged[int].connect(self.checkcombo) 

whatewer is the current state , just call a function which checks it and then based on its output enable/disable it

def checkcombo():
    if self.dlg.checkBox.isChecked():
        self.dlg.cmbvectorLayer6.setEnabled(True)
    else:
        self.dlg.cmbvectorLayer6.setEnabled(False)

Upvotes: 0

Achayan
Achayan

Reputation: 5895

if self.dlg.checkBox.isEnabled():
    self.dlg.cmbvectorLayer6.setEnabled(True)

You checking the state is checked but you need to check isEnabled

Upvotes: 0

Related Questions