|
@@ -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__
|
|
#endif // __COMMONFUNC_H__
|