Qt中QLineEdit如何限制小数的范围(QDoubleValidator失效下解决方法)
来源:互联网 发布:php unity3d 编辑:程序博客网 时间:2024/06/07 00:19
限制小数范围,乍看起来很简单,不是有QDoubleValidator吗?一用,没效果,我Qt版本5.6,其他版本什么情况就不知道了。。
百度,解决方法:继承QDoubleValidator,重写validate方法。
在Stackoverflow上看到的demo:
class MyDoubleValidator : public QDoubleValidator{public: MyDoubleValidator(double bottom, double top, int decimals, QObject *parent = 0) :QDoubleValidator( bottom, top, decimals, parent) {}; QValidator::State validate(QString &input, int &pos) const { /* * Original Qt Documentation: * --------- * QDoubleValidator::validate() * Returns Intermediate if input contains a double that is * outside the range or is in the wrong format; e.g. with too many * digits after the decimal point or is empty. * --------- * Problem: Not what the user expects. * Example: Range 0.0-10.0 with 1 digit (QDoubleValidator( 0, 10, 1, parent ) ): * QDoubleValidator::validate() reports intermediate for "10.3". * However we expect invalid instead and QLineEdit to decline input. * Fix this by overloading the validate() operator. */ const QValidator::State origState = QDoubleValidator::validate( input, pos ); if( ( origState == QValidator::Intermediate ) && ( input.toDouble() > top() ) ) { return QValidator::Invalid; } else { return origState; } }};
https://stackoverflow.com/questions/10119310/qdoublevalidator-is-not-working
但有问题,限制不了下限。
继续找:
csdn的blog上看到的:
virtual State validate(QString &str, int &i) const{if (str.isEmpty()){return QValidator::Intermediate;}int a = 1;bool cOK = false;double val = str.toDouble(&cOK);if (!cOK){return QValidator::Invalid;}int dotPos = str.indexOf(".");if (dotPos > 0){if (str.right(str.length() - dotPos-1).length() > decimals()){return QValidator::Invalid;}}if (val< top() && val > bottom()){return QValidator::Acceptable;}return QValidator::Invalid;}};
http://blog.csdn.net/caoqiang2006/article/details/53237188
问题又来了,小数位数限制不了了。
东拼西凑研究了一下,有个不完美的解决方案:
virtual QValidator::State validate(QString &input, int &pos) const { const QValidator::State origState = QDoubleValidator::validate( input, pos ); if(input.isEmpty()) { return QValidator::Intermediate; } if( ( origState == QValidator::Intermediate ) && (input.toDouble() > top()) ) { return QValidator::Invalid; } if(( origState == QValidator::Intermediate ) && ( input.toDouble() < bottom()) ) { return QValidator::Intermediate; } else { return origState /*QValidator::Acceptable*/; } }
不完美之处:如果下限设置为0.5,输入的时候0就无法输入了,也就是说[0.51-1)之间的数字都输入不了了。
解决了:1、能处理上下限;
2、可清空输入
由于我的需求里面小数都是0.5的整数倍,所以刚好能用,但还是有改善的空间。
尚待改善。。如果有更好的解决方法后续再更新。
Qt萌新一枚,如有不合理之处或有更好的解决方案,还望不吝教正。
阅读全文
1 0
- Qt中QLineEdit如何限制小数的范围(QDoubleValidator失效下解决方法)
- qt中限制QLineEdit的输入范围
- 限制QLineEdit的数值输入范围(QT正则表达式方法),顺便简单介绍下QT正则表达式方法
- 限制QLineEdit的数值输入范围
- 限制QLineEdit的数值输入范围
- 限制QLineEdit的数值输入范围
- 限制QLineEdit的数值输入范围
- 限制QLineEdit的数值输入范围
- 限制QLineEdit的数值输入范围
- QIntValidator 限制QLineEdit输入范围
- 学习记录-对Qt的QLineEdit进行输入字符限制
- Qt5.9.1 QDoubleValidator 直接构建失效的解决办法
- Qt中QLineEdit切换
- Qt窗体中含有QLineEdit的事件触发情况下的注意事项
- QT中QLineEdit的editingFinished()信号在按下回车时会触发两次的解决办法
- QT如何将QlineEdit里的QString字符串存储到unsigned char 数组中
- 如何在QT的ui界面上为QLineEdit文本框中建立事件
- 控制QLineEdit的输入范围
- FFmpeg源码剖析-通用:av_register_all()
- [HDU] 4336 Card Collector 期望dp
- 滴滴出行-前端面试题目
- Spring中@Autowired注解、@Resource注解的区别
- 文章标题
- Qt中QLineEdit如何限制小数的范围(QDoubleValidator失效下解决方法)
- noi2015 day1 T2软件包管理器
- 前端Js框架汇总
- 浅谈如何提高项目软件品质
- 360 video【待整理】
- Google chrome浏览器打不开网页,显示ERR_Failed...等问题的解决方法
- psql- 致命错误 角色 highggo 不存在
- 基于flask实现的简单博客
- 当pgpool两节点状态均显示standby时的解决方法1