[SOLVED] Calling Slots with value/parameter?
-
so hi again,
I need to call a SLOT with a parameter/value.
how to do this, if i set up like this, the slot is not called@for (int i=0; i< value_x; i++){
connect (push_button_here[i], SIGNAL(clicked()), this, SLOT(do_this(i)));
} @
how can this be fixed?
just tried solving this like forever.thank you already
-
Hi!
"QSignalMapper":http://doc.qt.digia.com/qt/qsignalmapper.html should do the trick.
Regards,
Jake -
Something like this should work, assuming you have a slot do_this(int) in your code.
@
QSignalMapper *signalMapper = new QSignalMapper(this);for (int i = 0; i < value_x; ++i) {
connect(push_button_here[i], SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(push_button_here[i], i);
}connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(do_this(int)));
@
Brain to keyboard, YMMV -
I can't find a project in which I played with QSignalMapper, but based on docs, it should be something like this:
@
QSignalMapper *mapper = new QSignalMapper(this);for(int i=0; i<0; i<value_x; i++)
{
connect(push_button_here[i], SIGNAL(clicked()), mapper, SLOT(map()));
mapper->setMapping(push_button_here[i], i);
}connect(mapper, SIGNAL(mapped(int)), this, SLOT(do_this(int)));@
Note: Not tested!
-
[quote author="mlong" date="1354648588"]Something like this should work, assuming you have a slot do_this(int) in your code.
@
QSignalMapper *signalMapper = new QSignalMapper(this);for (int i = 0; i < value_x; ++i) {
connect(push_button_here[i], SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(push_button_here[i], i);
}connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(do_this(int)));
@
Brain to keyboard, YMMV[/quote]WOW Thats amazing!
great working!
thanks a lot
-
Well... I did modify the example code :) .
Oh and one more solution I forgot to mention.
You could use sender() method ( in do_this(), without parameters), which returns QObject *. You can then cast it to QPushButton * or whatever widget you have. But I wouldn't recommend using it, as there might be problems if you call method directly or is accidentally called with signal from object with different type.
But might come in handy in a few lazy cases ;) .