当前位置:网站首页>Solution to the problem that qlineedit setting qdoublevalidator setting range is invalid
Solution to the problem that qlineedit setting qdoublevalidator setting range is invalid
2022-06-24 17:00:00 【Changsha Red fatty】
If the article is original article , Reprint please indicate original text Source
Qt Development column : All kinds of problems are solved ( Click on the portal )
problem
When developing , about QLineEdit Set its value to be limited and input range limited .
QLineEdit Set up QDoubleValidator Invalid , Set up QIntValidator It works .
Invalid code and valid code
#if 0
// Floating point invalid code
ui->lineEdit_t->setValidator(new DoubleValidator(40, 80, 1);
#else
// Floating point number valid code
QDoubleValidator * doubleValidator = new QDoubleValidator(40, 80, 1);
doubleValidator->setNotation(QDoubleValidator::StandardNotation);
ui->lineEdit_t->setValidator(doubleValidator);
#end
// Integer valid code
ui->lineEdit_coverT->setValidator(new QIntValidator(0, 110));
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
Add (2021 year 05 month 17 Japan )
The above methods , Indeed, only the entered digits can be displayed , Because I am a floating-point number within a limited range of integers , So ignore , To limit floating-point numbers with decimals , Overload it .
Particular attention : For example, restrictions 1.0-10, that QLineEdit After use, you can enter “10.”, As a string to double by 10, But pay attention when saving as a string . The problem of
#ifndef MYDOUBLEVALIDATOR_H
#define MYDOUBLEVALIDATOR_H
#include <QDoubleValidator>
class MyDoubleValidator: public QDoubleValidator
{
public:
MyDoubleValidator(double bottom, double top, int decimals, QObject * parent = 0) :
QDoubleValidator(bottom, top, decimals, parent)
{
}
QValidator::State validate(QString &s, int &i) const
{
if (s.isEmpty() || s == "-") {
return QValidator::Intermediate;
}
QChar decimalPoint = locale().decimalPoint();
if(s.indexOf(decimalPoint) != -1) {
int charsAfterPoint = s.length() - s.indexOf(decimalPoint) - 1;
if (charsAfterPoint > decimals()) {
return QValidator::Invalid;
}
}
bool ok;
double d = locale().toDouble(s, &ok);
if (ok && d >= bottom() && d <= top()) {
return QValidator::Acceptable;
} else {
return QValidator::Invalid;
}
}
};
#endif // MYDOUBLEVALIDATOR_H
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
If the article is original article , Reprint please indicate original text Source
边栏推荐
- Prometheus deployment
- Development analysis of main chain system
- zblog系统如何根据用户ID获取用户相关信息的教程
- Solution to the problem that kibana's map cannot render longitude and latitude coordinate data
- What does the router pin mean?
- [playing with Tencent cloud] a solution to the impassability of cross-border access to foreign websites using Tencent cloud CVM
- AI video structured intelligent security platform easycvr realizes intelligent security monitoring scheme for procuratorate building
- How Tencent cloud es achieves cross cluster data copy & lt through reindex; Lower & gt;
- Tencent blue whale Zhiyun community version v6.0.3 was officially released together with the container management platform!
- Heavy release! Tencent cloud ASW workflow, visual orchestration cloud service
猜你喜欢

A survey of training on graphs: taxonomy, methods, and Applications

A survey on model compression for natural language processing (NLP model compression overview)

MySQL learning -- table structure of SQL test questions

A survey on dynamic neural networks for natural language processing, University of California

Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)
![[leetcode108] convert an ordered array into a binary search tree (medium order traversal)](/img/e1/0fac59a531040d74fd7531e2840eb5.jpg)
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)
随机推荐
The RTSP video structured intelligent analysis platform easynvr stops calling the PTZ interface through the onvif protocol to troubleshoot the pending status
What is zero trust? Three classes will show you how to understand him!
Kubernetes 1.20.5 setting up Sentinel
API documents are simple and beautiful. It only needs three steps to open
实现TypeScript运行时类型检查
Experience and suggestions on cloud development database
Game business DDoS attack and defense confrontation case sharing
Principle analysis of robot hardware in the loop system
Mathematics in machine learning -- point estimation (IV): maximum posteriori probability (map)
What is the difference between get and post? After reading it, you won't be confused and forced, and you won't have to fight with your friends anymore
Druid architecture and Implementation
Abnormal dockgeddon causes CPU 100%
After the collective breakthrough, where is the next step of China's public cloud?
Object store signature generation
Development analysis of main chain system
Release! Tencent IOA and Tencent sky screen were selected into the first batch of certified products of domestic digital trusted services
About with admin option and with grant option
What is cloud development? Why cloud development? Talk about our story
主链系统发展解析
How to customize the log output format of zap?