Răsfoiți Sursa

V1.3.3
1、新增了限制QLineEdit的范围的类

Apple 3 zile în urmă
părinte
comite
7c186e24bf

+ 1 - 0
common/commonDefine.h

@@ -51,4 +51,5 @@
     }
 
 
+
 #endif /* COMMONDEFINE_H */

+ 4 - 0
common/commonFunc/commonFunc.cpp

@@ -0,0 +1,4 @@
+#include "commonFunc.h"
+
+
+

+ 43 - 0
common/commonFunc/commonFunc.h

@@ -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__