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 :
- QSpinBoxemit another- valueChangedafter I call its setValue, resulting in an infinite loop (until my- QVectorexplodes).
- I still have to find a way to keep track of Index, depending on which of the QSpinBoxarrows 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;
};
 
     
     
     
    