PDA

View Full Version : functions or values in signal-slot connections



davidovv
19th December 2011, 23:26
Hi

I am trying to create "elegant" way to connect 2 comboboxes.
If the selected item in first combobox is index = 3 i want to enable second combobox, for other items in first combobox the second combobox should be disabled.
Of course i could program that manually but... the code is better if there is less code :)
The "elegant" way i thought could work is to connect them something like this:

connect(combo1, SIGNAL(activated(int)), combo2 SLOT(setEnabled(bool)));

but the bool parameter in SLOT should be function of int from SIGNAL, like (int == 3)
Is there a way to achieve this

there is a similar thing on forum but it is for python, i am using c++;
http://www.qtcentre.org/threads/23827-Passing-values-to-custom-slot-functions-(pyqt)

Lesiok
20th December 2011, 08:01
Solution is QSignalMapper (http://doc.trolltech.com/4.7/qsignalmapper.html)

davidovv
20th December 2011, 16:58
this is the example from QSignalMapper help:

signalMapper = new QSignalMapper(this);

QGridLayout *gridLayout = new QGridLayout;
for (int i = 0; i < texts.size(); ++i) {
QPushButton *button = new QPushButton(texts[i]);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, texts[i]);
gridLayout->addWidget(button, i / 3, i % 3);
}

connect(signalMapper, SIGNAL(mapped(const QString &)),
this, SIGNAL(clicked(const QString &)));

I still dont see the solution, i have only one sender - combo1, and only one receiver - combo2.
The example shows one receiver and multiple senders.
And after, the last connection should have bool as function parameter, how do i get bool;.

nish
20th December 2011, 20:14
there would be only two lines of code in the slot connected to the activated() signal.

if ( index == 3 )
combo2->setEnabled(true)
else
combo2->setEnabled(false);

i think its not much of a code.

davidovv
20th December 2011, 21:16
there are also slot declaration, and slot definition lines, and the fact that the wrapped definition can be scattered with a lot of code... for this example 5 scattered lines against 1 readable line.
As i said before, i could do that, and i did it like this (see how i like to compact my code)

combo2->setEnabled(index == 3);

but i had a feeling that the it was possible, but not very "famous"
it would be great to have expressions in connections, like:

connect(combo1, SIGNAL(activated(int)), combo2 SLOT(setEnabled((bool)(int == 3)));

the connections are already possible with

connect(combo1, SIGNAL(activated(int)), combo2 SLOT(setSomethingWithoutArgument()));
connect(combo1, SIGNAL(activated(int)), combo2 SLOT(setSomething(long)));
(am i right?)

Anyway, thanks for your time

Lesiok
21st December 2011, 11:19
You can't use expressions in slot/signal definition. Just create new class inherited from QComboBox and define in them mySuperSlot(int) and mySuperSignal(bool). Then connect activated(int) to this new slot and in this slot emit mySuperSignal connected to another combo's slot setEnabled(bool).