Преглед на файлове

V1.4.3
1、添加了QLineEdit对整数的限制

Apple преди 2 седмици
родител
ревизия
d1977cbcf7
променени са 1 файла, в които са добавени 42 реда и са изтрити 0 реда
  1. 42 0
      common/commonFunc/commonFunc.h

+ 42 - 0
common/commonFunc/commonFunc.h

@@ -38,6 +38,48 @@ public:
     }
 };
 
+/* 限制Int范围 */
+class StrictIntValidator : public QIntValidator
+{
+public:
+    StrictIntValidator(int bottom, int top, QObject *parent = nullptr)
+        : QIntValidator(bottom, top, parent) {}
+
+    State validate(QString &input, int &pos) const override
+    {
+        // 允许空输入(方便编辑)
+        if (input.isEmpty() || input == "-")
+            return Intermediate;
+
+        bool ok = false;
+        int val = input.toInt(&ok);
+        if (!ok)
+            return Invalid;
+
+        /* 如果范围都是正数,只检查最大值 */
+        if(bottom() > 0)
+        {
+            if(val > top())
+                return Invalid;
+        }
+        else if(top() < 0) // 如果范围都是负数,只检查最小值
+        {
+            if(val < bottom())
+                return Invalid;
+        }
+        else // 范围有正有负,检查上下限
+        {
+            if(val < bottom() || val > top())
+                return Invalid;
+        }
+
+
+        return Acceptable;
+    }
+};
+
+
+
 
 
 #endif // __COMMONFUNC_H__