2

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...

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Eleftheria
  • 143
  • 2
  • 7

3 Answers3

2

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)

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
0
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)
  • maybe I did not write it well.. combobox is disabled not the checkbox.. but if the user wants to use the combobox first needs to check the checkbox so that the combobox is enabled – Eleftheria Nov 18 '15 at 18:48
  • sorry, i misread the question. what error did you get? based on your code it should work – user5509884 Nov 18 '15 at 18:53
  • thanx it is working and I also understand it.. just that in def checkcombo () need to pass self argument – Eleftheria Nov 18 '15 at 19:31
0
if self.dlg.checkBox.isEnabled():
    self.dlg.cmbvectorLayer6.setEnabled(True)

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

Achayan
  • 5,720
  • 2
  • 39
  • 61
  • the checkbox should be always enabled.. it is when it is checked that the disabled combobox should become enabled – Eleftheria Nov 18 '15 at 19:33