[Solved] SIGNAL and SLOTS
-
Hi all
I am wondering about SIGNAL and SLOTS
I have the following code:
@ for(int i = 0; i < NUMBERS; i++)
{
NumbersLabel[i] = new QLabel(" N" + QString::number(i+1));NumbersSpinBox[i] = new QSpinBox; NumbersSpinBox[i]->setRange(MINNUMBER, MAXNUMBER); NumbersSpinBox[i]->setSpecialValueText(" "); connect(NumbersSpinBox[i], SIGNAL(editingFinished()), this, SLOT(NumberEditingFinished(i))); hLayout->addWidget(NumbersLabel[i]); hLayout->addWidget(NumbersSpinBox[i]); }@
I know it doesn't work... But it illustrates what I want.
I want to connect editingFinished() from all my SpinBoxe's to the SLOT NumberEditingFinished(). I was hoping that I could send a argument to the SLOT function, i.e I want to know who is emitting the signal.
How can I do this ? do I have to make my own SIGNAL somehow ? Or is there another way to do this ?
Thanks in advance
DarkRoast -
Hi!
If you just want to know which QObject (or QSpinBox in your example) is emitting the signal, just try
"QObject::sender()":http://doc.qt.nokia.com/4.7/qobject.html#sender.@void MyObject::NumberEditingFinished
{
QObject* emittingObject = sender();
if(emittingObject == 0)
{
//slot is NOT activated by a signal
}
else
{
//slot is activated by a signal emitted from emittingObject
}
}@ -
I'm not sure i got your problem correctly, but signal mapper looks like what you are looking for.
In "this":http://doc.qt.nokia.com/4.7/signalsandslots.html page you have documentation for signals and slots, including default parameters and signal mapper.
-
Hi DarkRoast,
There are some things to tell you:
a slot might never have more parameters than the signal cobnnected to it, less is ok, but not more.
in a connect you can't define a value for a parameter, as the parameter of a slot is filled by the signal parameters
As Zlatomir already said, "QSignalMapper":http://doc.qt.nokia.com/4.7/qsignalmapper.html might solve your problem (or QObject::sender() which gives you a pointer to the signal sender object).
Once again:
It is ok to have signalA(class1, class2) connected to
- slotA(class1, class2)
- slotB()
- slotC()
but not to
- slotA(class1, class2, class3)
-
Thanks for all your replies!
I ended with using QSignalMapper, as seen in code beneath:
@ QSignalMapper *signalMapper = new QSignalMapper(this);
connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(NumberEditingFinished(int)));for(int i = 0; i < NUMBERS; i++) { NumbersLabel[i] = new QLabel(" N" + QString::number(i+1)); NumbersSpinBox[i] = new QSpinBox; NumbersSpinBox[i]->setRange(MINNUMBER, MAXNUMBER); NumbersSpinBox[i]->setSpecialValueText(" "); signalMapper->setMapping(NumbersSpinBox[i], i); connect(NumbersSpinBox[i], SIGNAL(editingFinished()), signalMapper, SLOT(map())); hLayout->addWidget(NumbersLabel[i]); hLayout->addWidget(NumbersSpinBox[i]); }@
This way my NumberEditingFinished(int) function always knows who is emitting the signal.
Thanks