PDA

View Full Version : How to enable objects based on a selection from a drop box.



pixxyman
16th January 2015, 16:22
Hi,

I have a tabWidget with a drop down box and 8 checkboxes.

The drop down has 4 items in it, i'd like to link this to the checkboxes, so..
1) All checkboxes are disabled to start with.
2) If I select ItemA in the drop down then checkbox's 1 and 2 are enabled.
3) If I select ItemB in the drop down then checkbox's 3 and 4 are enabled and so on.

How do I do this in Qt desigener or must I do this problematically?

I'm using Python 3.4 and Qt Designer 4.8.6.

Thanks,

John.

anda_skoa
16th January 2015, 16:27
You need a slot that is connected to the combobox's currentIndexChanged() signal and in there enable/disable buttons as you see fit.

Cheers,
_

pixxyman
16th January 2015, 17:11
Thanks,

However, If I select Edit Slots/Signals and drag an arrow from my combo box to checkbox 1. I then have this in the Signal/Slot Editor
Sender Signal Receiver Slot
comboBox currentIndexChanged(int) checkBox_A click() or show()

But it just toggles check or uncheck on the checkboxes. I don't see any option where if select Item A, Item B etc from the combo box.

Likewise, if I go in reverse (dragging an arrow from the checkbox to the combo box).

Am i missing something?

Thanks.

anda_skoa
16th January 2015, 17:59
Am i missing something?

Yes, the part of where I wrote that you need a slot that does the enabling/disabling based on the index value :)

Cheers,
_

yeye_olive
16th January 2015, 18:05
You are trying to connect the combobox' currentIndexChanged(int) signal to a slot in one of the checkboxes. This is not a correct design: the action you need to perform is not local to one checkbox, so it should have a larger context. A simple solution is to put the slot in the class for the window containing your widgets, connect currentIndexChanged(int) to this slot, and write code in the slot to change the states of all the checkboxes.

d_stranz
16th January 2015, 19:36
You could try doing what yeye_olive suggests in Qt Designer, but it is probably easier to simply do it in the code for your tab widget class. In any case, you will still need to write the code for the slot - there is nothing in Designer that will let you graphically build the signal / slot logic to conditionally enable UI elements based on a combobox selection.

pixxyman
19th January 2015, 15:26
ok, so my slot is going to be a method thats going to enable the checkboxes based on which value I select from the combo box?

yeye_olive
19th January 2015, 15:50
ok, so my slot is going to be a method thats going to enable the checkboxes based on which value I select from the combo box?
Yes, it could look like this:


void MyTabWidget::updateCheckboxes(int comboBoxIndex) {
checkBox1->​setEnabled(/* Some boolean condition that depends on comboBoxIndex */);
/* ... */
checkBox8->​setEnabled(/* Some other boolean condition that depends on comboBoxIndex */);
}

Note: since the beginning you have been talking about enabling/disabling checkboxes, which means controlling whether the user may interact with them; this is what QWidget::setEnabled() does. If you meant checking/unchecking the checkboxes, then you'll need to call QCheckBox::​setCheckState() instead.

pixxyman
19th January 2015, 16:34
Yes, this is correct. If the user select ItemA from the drop down. I only want them to be able to select checkbox 1 or 2 and all the others would be disabled.

Am i correct in thinking if I create signal with currentIndexChanged(), its not enough to just do that. As it will disable if I select any option. I need to say if currentIndexChanged() changed to ItemA, then enable checkbox 1 and 2.

d_stranz
19th January 2015, 19:55
You do not create the currentIndexChanged() signal - the combo box emits that. You need to connect your own slot to it, and in the slot you need to do something like this:



// constructor
MyTabWidget::MyTabWidget( QWidget * parent )
: QTabWidget( parent )
{

// setupUi, etc...

connect( myComboBox, &QComboBox::currentIndexChanged, this, &MyTabWidget::updateCheckboxes );

}

// slot
void MyTabWidget::updateCheckboxes(int comboBoxIndex)
{
switch( comboBoxIndex )
{
case 0:
// enable checkboxes base on selection of first item in combobox
break;

case 1:
// enable checkboxes base on selection of second item in combobox
break;

// etc.
default:
break;
}
}


You might have to initialize things by calling updateCheckboxes( 0 ) in the constructor (using whatever the default startup index should be from the combobox). This makes sure that when the app first comes up, the checkboxes are in the correct states for whatever item appears by default in the combobox. If you set the initial index to -1 (no selection), then you will probably want to disable all of the checkboxes.

If your combobox does not have a fixed number of items in it (for example, the choices are loaded from a file, database, or are determined in some other way), then obviously a switch statement won't work unless the "default:" clause is very smart. It does not sound like this is your problem though.

pixxyman
21st January 2015, 09:57
Thanks for the help. I have this working now.

// def setupUi(self, qadashboard): etc..
QtCore.QObject.connect(self.comboBox, QtCore.SIGNAL(_fromUtf8("currentIndexChanged(int)")), self.showMe)

// slot
def showMe(self):
self.index = self.comboBox.currentIndex()
if (self.index is 1):
self.checkBox_1.setEnabled(1)
self.checkBox_2.setEnabled(1)
self.checkBox_3.setDisabled(1)
self.checkBox_4.setDisabled(1)

elif (self.index is 2):
self.checkBox_3.setEnabled(1)
self.checkBox_4.setEnabled(1)
self.checkBox_1.setDisabled(1)
self.checkBox_2.setDisabled(1)

etc..

def retranslateUi(self, qadashboard):
I set everything to disabled in here.

So it all works as i'd like it to. Thanks for the help.