24 lines
743 B
C++
24 lines
743 B
C++
|
|
#ifndef _CUSTOM_DOUBLE_SPIN_BOX_H_
|
|
#define _CUSTOM_DOUBLE_SPIN_BOX_H_
|
|
|
|
#include <QDoubleSpinBox>
|
|
#include <QString>
|
|
|
|
class CustomDoubleSpinBox : public QDoubleSpinBox {
|
|
public:
|
|
explicit CustomDoubleSpinBox(QWidget *parent = nullptr) : QDoubleSpinBox(parent) {
|
|
// 设置一个较大的最大位数,让用户有足够的输入精度
|
|
setDecimals(10);
|
|
}
|
|
|
|
protected:
|
|
// 重写此方法,自定义数值如何显示在输入框中
|
|
QString textFromValue(double value) const override {
|
|
// 使用 'g' 格式可以自动去掉末尾不必要的零
|
|
// 或者使用 QString::number 配合手动处理
|
|
return QString::number(value, 'g', decimals());
|
|
}
|
|
};
|
|
|
|
#endif // _CUSTOM_DOUBLE_SPIN_BOX_H_
|