c++qtsignalsslotqspinbox

How to get the value of a spin box just before it changes


Using Qt, I need to get the value of a spin box (by a signal) just before it changes.

I've got:

QSpinBox spinBoxWidth:
QSpinBox spinBoxScale;

I want to connect a signal from spinBoxWidth to spinBoxScale, so that the value of spinBoxScale is always "the Value of spinBoxWidth after changing", to "its value before changing".

Scale = width_new / width_old

I didn't find any slot in Qt which returns the old value of a spin box while changing the value. Can I somehow write a slot for that?


Solution

  • There are two ways of doing this:

    Try something like this:

    class MonitoringObject : public QObject
    {
        Q_OBJECT
        int lastValue;
        int currentValue;
        ...
    public Q_SLOTS:
        void onValueChanged(int newVal)
        {
            lastValue = currentValue;
            currentValue = newVal;
            if (lastValue == 0) //catch divide-by-zero
                emit ratioChanged(0);
            else
                emit ratioChanged(currentValue/lastValue);
        }
    Q_SIGNALS:
        void ratioChanged(int);
    

    After your signals are connected, the flow should look like this: