Set specific values in a QSpinBox

CTZStef picture CTZStef · Nov 29, 2013 · Viewed 8.4k times · Source

I have a widget containing a QSpinBox. This widget also has a QVector<int> Values. What I would like to do is get the QSpinBox to display values issued from Values only.

At first I thought that a new slot and signal in my widget would do the trick, something like

slot :

void ChangeSomeValue()
{
    // QVector<int> Values;
    // int Index;
    int val = Values[ Index ];
    emit( SomeValueChanged( val ) );
}

connects :

connect( UI->MySpinBox, SIGNAL( valueChanged(int) ), this, SLOT( ChangeSomeValue() ) );
connect( this, SIGNAL( SomeValueChanged(int ) ), UI->MySpinBox, SLOT( setValue(int) ) );

But then several problems arise :

  1. QSpinBox emit another valueChanged after I call its setValue, resulting in an infinite loop (until my QVector explodes).
  2. I still have to find a way to keep track of Index, depending on which of the QSpinBox arrows was clicked (up or down... I don't even think this is possible).

So my solution, with its problems, seems to be a dead end. Any idea?. I'd like to stick to QSpinBox, if possible.

Thanks !

[EDIT]

Subclassing :

class SpinBox : public QSpinBox
{
    Q_OBJECT
public:
    explicit SpinBox(const QVector<int> & values, QWidget * parent = 0) :
        QSpinBox(parent),
        mValues(values),
        mIndex(0)
    {
        qSort(mValues);
        setMinimum(mValues.at(0));
        setMaximum(mValues.at(mValues.size() - 1));
        setValue(mValues.at(0));
    }

protected:
    void stepBy(int steps) // re-implementaion
    {
        mIndex += steps;
        mIndex = qBound(0, mIndex, mValues.size() - 1);
        setValue(mValues.at(mIndex));
    }

private:
    QVector<int>    mValues;
    int             mIndex;
};

Answer

Pete picture Pete · Nov 29, 2013

I Would suggest writing your own class to do it by sub-classing QAbstractSpinBox.

Maybe take a look at the accepted answer on this question: How to subclass QSpinBox so it could have int64 values as maxium and minimum