Reputation: 6299
With Qt 4.8 I create a number of input widgets (QSpinBox, QSlider) programmatically. In the end, I would like to have a single method to handle changes of any of these input widgets, ideally by index.
However, these widgets only have a Signal with parameter, e.g. valueChanged(int)
.
This is not compatible with QSignalMapper()'s Slot map()
.
As it was pointed out in the comments, the connection does work!
connect( spinbox, SIGNAL( valueChanged(int) ),
signalMapper, SLOT( map() )
);
Now I just need to get the value, but this cannot be done via the sender() method anymore, because this now is the SignalMapper.
Is there another way besides (re)implementing QSignalMapper
with additional parameters or a parameter-less valueChanged()
for the widget or using objectName
and QObject::sender()
in order for the Slot to see which element changed (and get the new value)?
Upvotes: 2
Views: 1801
Reputation: 101
If I understand your question and what you have until now correctly, you are missing two parts.
mapper->setMapping(<spinbox>, <id or name or pointer to the spinbox);
connect(mapper, SIGNAL(mapped(<datatype you used in setMapping()>),
this, SLOT(HandleValueChanged(<datatype you used in setMapping()>)));
So the HandleValueChanged()
slot will receive an identifier of your sender, then you can directly access the value of the sender with the appropriate getter.
The method setMapping()
takes either and integer, QString or a pointer to the widget itself as second argument. This is then forwarded via the mapped() signal of the mapper, so you can later identify which widget emitted the signal.
Upvotes: 0
Reputation: 9691
You can use QAbstractSpinBox::editingFinished()
and QAbstractSlider::sliderReleased()
as your signals, they are parameterless.
Unfortunately there is no parameterless version of QAbstractSlider::valueChanged()
so if you want a signal emitted continuously as the slider moves, you may need to subclass QSlider and create it. E.g.
class MySlider : public QSlider
{
...
private slots:
void HandleValueChanged(int) { emit valueChanged(); }
signals:
void valueChanged();
};
MySlider::MySlider(...)
{
connect(this, SIGNAL(valueChanged(int)), this, SLOT(HandleValueChanged(int)));
}
Though I admit, this may not be the most elegant solution.
Upvotes: 1