The third option will be to subclass QSpinBox, implement the necessary functionality there and use the derived class instead of QSpinBox - this hides all the associated complexity in the derived class and allows you to use it in the same way as QSpinBox.
For example, the following class
myQSpinBox.h
#ifndef MYQSPINBOX_H #define MYQSPINBOX_H #include <QSpinBox> class myQSpinBox : public QSpinBox { Q_OBJECT public: myQSpinBox(QWidget * parent = 0 ); protected: bool valueBeingSet; public slots: void setValue (int val); private slots: void On_valueChanged(int val); signals: void valueChangedNotBySet(int val); }; #endif // MYQSPINBOX_H
myQSpinBox.cpp
#include "myQSpinBox.h" myQSpinBox::myQSpinBox(QWidget * parent) : QSpinBox(parent) , valueBeingSet(false) { connect(this,SIGNAL(valueChanged(int)),this,SLOT(On_valueChanged(int))); } void myQSpinBox::setValue ( int val ) { valueBeingSet = true; QSpinBox::setValue(val); valueBeingSet = false; } void myQSpinBox::On_valueChanged(int val) { if(!valueBeingSet) emit valueChangedNotBySet(val); }
will highlight valueChangedNotBySet(int); in cases 1 and 2., but not in case 3., retaining all the functionality of QSpinBox
Ilya Kobelevskiy
source share