|
@@ -0,0 +1,43 @@
|
|
|
|
+#ifndef __COMMONFUNC_H__
|
|
|
|
+#define __COMMONFUNC_H__
|
|
|
|
+
|
|
|
|
+#include <QDoubleValidator>
|
|
|
|
+
|
|
|
|
+/*--------------------------------------------------------------------------
|
|
|
|
+ * 公共小函数,解决一些通用的bug
|
|
|
|
+ *--------------------------------------------------------------------------*/
|
|
|
|
+
|
|
|
|
+/* 严格范围限制的 QDoubleValidator 解决原有的不是限制范围的bug */
|
|
|
|
+class StrictDoubleValidator : public QDoubleValidator
|
|
|
|
+{
|
|
|
|
+public:
|
|
|
|
+ StrictDoubleValidator(double bottom, double top, int decimals, QObject *parent = nullptr)
|
|
|
|
+ : QDoubleValidator(bottom, top, decimals, parent) {}
|
|
|
|
+
|
|
|
|
+ State validate(QString &input, int &pos) const override
|
|
|
|
+ {
|
|
|
|
+ // 允许空输入(方便编辑)
|
|
|
|
+ if (input.isEmpty() || input == "-" || input == "." || input == "-.")
|
|
|
|
+ return Intermediate;
|
|
|
|
+
|
|
|
|
+ bool ok = false;
|
|
|
|
+ double val = input.toDouble(&ok);
|
|
|
|
+ if (!ok)
|
|
|
|
+ return Invalid;
|
|
|
|
+
|
|
|
|
+ // 检查小数位数
|
|
|
|
+ int dot = input.indexOf('.');
|
|
|
|
+ if (dot >= 0 && input.length() - dot - 1 > decimals())
|
|
|
|
+ return Invalid;
|
|
|
|
+
|
|
|
|
+ // 范围检查
|
|
|
|
+ if (val < bottom() || val > top())
|
|
|
|
+ return Invalid;
|
|
|
|
+
|
|
|
|
+ return Acceptable;
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#endif // __COMMONFUNC_H__
|