Here’s a sample of a SpinBox that writes its changes to underlying variables. The main problem that I’m having is valueChanged is called when the widget is constructed. Is there a more elegant way to do this? I think it’s weird that I connected a widget to itself, but valueChanged isn’t virtual.
class ValueWriterInt: public QSpinBox {
Q_OBJECT
public:
ValueWriterInt(vector<int*> const& value): myValue(value) {
QObject::connect(this, SIGNAL(valueChanged(int)), this, SLOT(valueChanged(int)));
}
~ValueWriterInt() {}
private slots:
void valueChanged(int new_value) {
for (auto it = myValue.begin(); it != myValue.end(); ++it)
**it = new_value;
}
private:
vector<int*> myValue;
};
I see nothing particularly weird about connecting a widget to itself. Having a single method of detecting and responding to data updates actually sounds like a good thing because you have fewer points of failure to check when you are debugging. In your specific case, it is causing some undesired behavior, but in general it is a fine solution.
Now, having expressed the opinion that a reflexive connection isn’t inherently inelegant, I am going to suggest a less than “elegant” solution to prevent the calling of
valueChangedafter construction. You can have a flag to determine whether the object was just constructed and return early to prevent the code being run immediately after construction. In your example:That isn’t too bad of a solution. It will at least give you your desired behavior until (and if) you can find a more elegant method.