I'm using lot of QDoubleSpinBoxes in my project (with Qt 4.8.0), and for all of them, I want the same range, single step size, value etc., which are different from the default values though.
I wanted to ask: Is there a way to change these default values so that new instances of the QSpinBoxes will be created with the new default values so that I don't to have change in every time?
Simply said, instead of this:
QDoubleSpinBox *spin1 = new QDoubleSpinBox(this);
spin1->setSingleStep(0.03);
spin1->setDecimals(4);
spin1->setRange(2.0, 35.0);
QDoubleSpinBox *spin2 = new QDoubleSpinBox(this);
spin2->setSingleStep(0.03);
spin2->setDecimals(4);
spin2->setRange(2.0, 35.0);
...
I want something like this:
QDoubleSpinBox::setDefaultSingleStep(0.03);
QDoubleSpinBox::setDefaultDecimals(4);
QDoubleSpinBox::setDefaultRange(2.0, 35.0);
QDoubleSpinBox *spin1 = new QDoubleSpinBox(this);
QDoubleSpinBox *spin2 = new QDoubleSpinBox(this);
Does anyone know if this is possible and if yes, how?
You can make a factory, that creates the spin boxes with the desired values.
e.g.
class MySpinBoxFactory {
public:
MySpinboxFactory(double singleStep, int decimals, double rangeMin, double rangeMax)
: m_singleStep(singleStep), m_decimals(decimals), m_rangeMin(rangeMin), m_rangeMax(rangeMax) {}
QDoubleSpinBox *createSpinBox(QWidget *parent = NULL) {
QDoubleSpinBox *ret = new QDoubleSpinBox(parent);
ret->setSingleStep(m_singleStep);
ret->setDecimals(m_decimals);
ret->setRange(m_rangeMin, m_rangeMax);
return ret;
}
private:
double m_singleStep;
int m_decimals;
double m_rangeMin;
double m_rangeMax;
}
// ...
MySpinboxFactory fac(0.03, 4, 2.0, 35.0);
QDoubleSpinBox *spin1 = fac.createSpinBox(this);
QDoubleSpinBox *spin2 = fac.createSpinBox(this);
You can also add setters to change the values. With this, you can create spinboxes with different default values using a single factory instance.
MySpinboxFactory fac(0.03, 4, 2.0, 35.0);
QDoubleSpinBox *spin1 = fac.createSpinBox(this);
QDoubleSpinBox *spin2 = fac.createSpinBox(this);
fac.setSingleStep(0.1);
QDoubleSpinBox *spin3 = fac.createSpinBox(this);