QLineEdit限定只能输入整数

news/2024/9/27 17:37:50

QLineEdit限定输入整数时遇到的问题

问题

QValidator常常用于判断数据的合法性,QLineEdit,QSpinBox,QComboBox中经常使用该类。

QLineEdit要限制输入整数,那么添加语句lineEdit->setValidator(new QIntValidator(100, 1000, this)),理论上可以实现限定只能输入100-1000的整数,但是经过测试发现输入0依旧是可以的,这是什么原因呢?

查阅官方文档,有关QValidator的详细描述是这么说的:

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.

这段文字大致说的是使用QLineEdit,用户按下回车或是文本框中的当前内容是无效的时候,QLineEdit会调用fixup()函数去试图纠正这个错误,但是QValidator中并没有实现fixup()函数,QValidator的派生类QIntValidator也并没有实现该函数,因此该问题的方法就是通过创建一个类去继承QIntValidator。

代码

myintvalidator.h

#ifndef MYINTVALIDATOR_H
#define MYINTVALIDATOR_H
#include <QIntValidator>class MyIntValidator : public QIntValidator
{Q_OBJECT
public:explicit MyIntValidator(QObject *parent = nullptr);MyIntValidator(int bottom, int top, QObject *parent);void setRange(int bottom, int top) override;	//设置整数的范围virtual State validate(QString &input, int &pos) const override;    //pos:the cursor position光标位置virtual void fixup(QString &input) const override;
};#endif // MYINTVALIDATOR_H

myintvalidator.cpp

#include "myintvalidator.h"MyIntValidator::MyIntValidator(QObject *parent): QIntValidator(parent)
{
}MyIntValidator::MyIntValidator(int bottom, int top, QObject *parent): QIntValidator(bottom, top, parent)
{
}void MyIntValidator::setRange(int bottom, int top)
{QIntValidator::setRange(bottom, top);
}QValidator::State MyIntValidator::validate(QString &input, int &pos) const
{return QIntValidator::validate(input, pos); //调用基类的validate
}//出现无效值或按下Enter键,QLineEdit会自动调用fixup()
void MyIntValidator::fixup(QString &input) const
{input = QString("%1").arg(bottom());    //出现无效值时使用下限去代替无效值
}

调用MyIntValidator类:lineEdit->setValidator(new MyIntValidator(1000, 1000000, this))

测试之后,当用户按下Enter时或是取消选中QLineEdit才会实现数据合法性的检测并使用下限值1000去代替

参考链接:

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

https://doc.qt.io/qt-5/qvalidator.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/45920.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Sql注入之WAF绕过

WAF 拦截原理 WAF 绕过的思路就是让 WAF 的检测规则,识别不到你所输入的敏感字符,利用上述所介绍的知识点,灵活结合各种方法,从而可以增加绕过 WAF 的可能性.1. 关键词大小写绕过 有的WAF因为规则设计的问题,只匹配纯大写或纯小写的字符,对字符大小写混写直接无视,这时,…

【持续更新】重要FLIP总结

FLIP-27: Refactor Source Interface 流批一体API 1、解耦SplitEnumerator与SplitReader SplitEnumerator:发现并分配splits(比如files/kafka_partitions) SourceReader:从splits里实际读取数据 这样就使不同的splits分配策略与读取动作解耦,可分别封装成两个组件,Source…

Java-HashMap中put源码解读

1.背景Map类型 优点 缺点 线程安全性HashMap 1. 查询、插入、删除操作的时间复杂度为O(1)。2. 允许键和值为null。 1. 无序,不保证迭代顺序。2. 不是线程安全的。 LinkedHashMap 1. 保留插入顺序或访问顺序。2. 与HashMap性能相似。 1. 内存开销较高,因为维护了一个双向链表。…

大模型显存计算

大模型微调需要多少GPU显存?如:微调 1B 模型,16bit = 2byte全量微调显存占用分为:model weight(参数本身):10亿(bit) = 20亿(byte)约等于2GB训练模型时,通过一系列反向传播的方法,来更新模型参数,涉及以下gradient​和optimizer states​参数。不断计算梯度,以更…

一图看懂编码,加密,令牌化的不同之处

一图看懂编码,加密,令牌化的不同之处

解决HBuilder X识别不了魅族手机的问题

似乎魅族手机有点特别,别的手机识别没事,但是到了魅族手机就是识别不了,下面上处理方案 这里假设你的调试已经打开的情况下, 找到目录C:\Users\tutu-qiuxie\Downloads\HBuilderX\plugins\launcher-tools\tools\adbs先把软件关闭, 打开HbuilderX后,记得手机上弹出的是否允…

[模式识别复习笔记] 第5章 贝叶斯分类器

1. 贝叶斯分类器 1.1 贝叶斯公式 假设有一个试验的样本空间为 \(S\),记 \(B_1, B_2, \ldots, B_c\) 为 \(S\) 的一个划分,\(A\) 为试验的条件,且 \(P(A) \not = 0\),则: \[P(B_i | A) = \frac{P(B_i)P(A|B_i)}{P(A)} = \frac{P(B_i)P(A|B_i)}{\sum_{j=1}^{c}P(B_j)P(A|B_j…

Windows删除服务

删除的是文件管理器里的服务 如何进入右击此电脑,选择管理(没看到管理的话点击显示更多选项) 进入计算机管理进入计算机管理后选择服务和应用程序 双击服务 要删除的就是这里的服务——————————如何删除 参考—— https://www.sysgeek.cn/windows-delete-service/以…