[英]Qt4: How to change the default values (range, value, single step size) for QSpinBox for new instances?
我在我的項目(使用Qt 4.8.0)中使用了許多QDoubleSpinBoxes,對於所有這些,我都希望具有相同的范圍,單個步長,值等,盡管它們與默認值不同。
我想問:有沒有辦法更改這些默認值,以便用新的默認值創建新的QSpinBoxes實例,這樣我就不必每次都更改了?
簡單地說,代替這個:
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);
...
我想要這樣的東西:
QDoubleSpinBox::setDefaultSingleStep(0.03);
QDoubleSpinBox::setDefaultDecimals(4);
QDoubleSpinBox::setDefaultRange(2.0, 35.0);
QDoubleSpinBox *spin1 = new QDoubleSpinBox(this);
QDoubleSpinBox *spin2 = new QDoubleSpinBox(this);
有誰知道這是否可能,如果可以,怎么辦?
您可以創建一個工廠,以創建具有所需值的旋轉框。
例如
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);
您還可以添加設置器來更改值。 這樣,您可以使用單個工廠實例創建具有不同默認值的旋轉框。
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);
您應該使自己的新類派生自QDoubleSpinBox。 在您的類構造函數中,設置所需的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.