当前位置:网站首页>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
边栏推荐
- Complete the log service CLS questionnaire in 1 minute and receive the Tencent cloud 30 yuan threshold free voucher ~
- Tencent security officially released the IOT security capability map
- Markdown syntax -- Formula
- 2021-04-02: given a square or rectangular matrix, zigzag printing can be realized.
- A survey of training on graphs: taxonomy, methods, and Applications
- What does the router pin mean?
- Example description and case of ansible playbook automated cluster server management
- Factory mode
- Audio knowledge (I)
- 实现TypeScript运行时类型检查
猜你喜欢

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

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

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

MySQL learning -- table structure of SQL test questions

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)
随机推荐
06. Tencent cloud IOT device side learning - Introduction to basic functions
One article combs multi task learning (mmoe/ple/dupn/essm, etc.)
未来银行需要用明天的思维,来思考今天架构
What is zero trust? Three classes will show you how to understand him!
zblog系统如何根据用户ID获取用户相关信息的教程
Can you remember the code of a programming boss? Can you hit it out without Baidu?
Development of block hash game guessing system (mature code)
Bypass kernel function pointer integrity check
Hook graphics kernel subsystem
Tencent cloud database mysql:sql flow restriction
A tutorial on how the zblog system obtains user related information based on user ID
[play with Tencent cloud] TSF User Guide
Introduction to koa (III) koa routing
## Kubernetes集群中流量暴露的几种方案 Kubernetes集群中流量暴露的几种方案
Principle analysis of robot hardware in the loop system
[web] what happens after entering the URL from the address bar?
Try catch finally implementation mechanism
Pagoda activities, team members can enjoy a lightweight server 1 core 2g5m 28 yuan for two years
Tencent security officially released the IOT security capability map
A very good educational man and resource center planning scheme, with word file download