• QIntValidator没有最小值的限制,继承然后写个新类来控制最小值


    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
    本文链接:https://blog.csdn.net/firecityplans/article/details/99440955
    问题:

    使用QIntValidator限制QLineEdeit整数输入的最大最小值时,如设置为[20,300],发现最小值仍然可以设置为20以下。
    QValidator类相关说明:

    QValidator类提供一种验证输入是否有效的方法。

    QValidator提供validate和fixup的公有方法:

        virtual State validate(QString &, int &) const = 0;  
        virtual void fixup(QString &) const;  

     QValidator::validate()是验证的过程,返回结果是State。每当输入有变化时调用此方法。

    State验证的结果有三种状态:

        enum State {  
            Invalid,        //验证通不过  
            Intermediate,   //输入未完成,不确定是否能通过验证  
            Acceptable      //验证通过
        }

     当验证通不过时,通过调用 QValidator::fixedup()是这个函数修复错误。

    然而,QIntValidator中的fixup()函数默认是未作任何处理的。fixup()的解释如下:

    fixup() is provided for validators that can repair some user errors. The default implementation does nothing. QLineEdit, for example, will call fixup() if the user presses Enter (or Return) and the content is not currently valid. This allows the fixup() function the opportunity of performing some magic to make an Invalid string Acceptable.

     因此,若要实现一个自定义的验证类,需要从QValidator下派生一个类,并选择性重写validate和fixedup方法。

     
    自己重新实现下QIntValidator即可:

         
        class MyValidator : public QIntValidator
        {
            Q_OBJECT
        public:
            explicit MyValidator(QObject * parent = 0):QIntValidator(parent)
            {
            }
            MyValidator(int bottom, int top, QObject * parent):QIntValidator(bottom, top, parent)
            {
            }
            virtual void setRange(int bottom, int top)
            {
                QIntValidator::setRange(bottom, top);
            }
            ~MyValidator()
            {
            }
            virtual State validate(QString &s, int &n) const
            {
                return QIntValidator::validate(s, n);
            }
            virtual void fixup(QString &s) const
            {
                s = QString("%1").arg(bottom());
            }
        };
         

    参考文章:

    https://bbs.csdn.net/topics/350177184

    https://blog.csdn.net/hfhuazai/article/details/86711903
    ————————————————
    版权声明:本文为CSDN博主「firecityplans」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/firecityplans/article/details/99440955

  • 相关阅读:
    extjs 获取Dom对象
    转: python requests的安装与简单运用
    转: python如何安装pip和easy_installer工具
    转: windows下面安装Python和pip终极教程
    JS Json数据转换
    转:永久解决火狐浏览器出现的flash版本更新问题
    python 字符编码 转换
    opencv输出图片像素值
    Mac中安装tensorflow(转)
    在linux和Mac中访问某个文件夹中所有的文件
  • 原文地址:https://www.cnblogs.com/liujx2019/p/11819564.html
Copyright © 2020-2023  润新知