C++ 将 QLineEdit 设置为仅接受数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13422995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set QLineEdit to accept only numbers
提问by sashoalm
I have a QLineEdit
where the user should input only numbers.
我有一个QLineEdit
用户应该只输入数字的地方。
So is there a numbers-only setting for QLineEdit
?
那么是否有仅数字设置QLineEdit
?
回答by Chris
QLineEdit::setValidator()
, for example:
QLineEdit::setValidator()
, 例如:
myLineEdit->setValidator( new QIntValidator(0, 100, this) );
or
或者
myLineEdit->setValidator( new QDoubleValidator(0, 100, 2, this) );
See: QIntValidator, QDoubleValidator, QLineEdit::setValidator
回答by ImmortalPC
The best is QSpinBox
.
最好的是QSpinBox
。
And for a double value use QDoubleSpinBox
.
对于双值使用QDoubleSpinBox
.
QSpinBox myInt;
myInt.setMinimum(-5);
myInt.setMaximum(5);
myInt.setSingleStep(1);// Will increment the current value with 1 (if you use up arrow key) (if you use down arrow key => -1)
myInt.setValue(2);// Default/begining value
myInt.value();// Get the current value
//connect(&myInt, SIGNAL(valueChanged(int)), this, SLOT(myValueChanged(int)));
回答by TrebledJ
The Regex Validator
正则表达式验证器
So far, the other answers provide solutions for only a relatively finitenumber of digits. However, if you're concerned with an arbitraryor a variablenumber of digits you can use a QRegExpValidator
, passing a regex that only accepts digits (as noted by user2962533's comment). Here's a minimal, complete example:
到目前为止,其他答案仅提供了相对有限数量的数字的解决方案。但是,如果您关心任意或可变数量的数字,您可以使用 a QRegExpValidator
,传递一个只接受数字的正则表达式(如user2962533 的评论所述)。这是一个最小的完整示例:
#include <QApplication>
#include <QLineEdit>
#include <QRegExpValidator>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLineEdit le;
le.setValidator(new QRegExpValidator(QRegExp("[0-9]*"), &le));
le.show();
return app.exec();
}
The QRegExpValidator
has its merits (and that's only an understatement). It allows for a bunch of other useful validations:
该QRegExpValidator
有它的优点(这只是一个保守的)。它允许进行一系列其他有用的验证:
QRegExp("[1-9][0-9]*") // leading digit must be 1 to 9 (prevents leading zeroes).
QRegExp("\d*") // allows matching for unicode digits (e.g. for
// Arabic-Indic numerals such as ???).
QRegExp("[0-9]+") // input must have at least 1 digit.
QRegExp("[0-9]{8,32}") // input must be between 8 to 32 digits (e.g. for some basic
// password/special-code checks).
QRegExp("[0-1]{,4}") // matches at most four 0s and 1s.
QRegExp("0x[0-9a-fA-F]") // matches a hexadecimal number with one hex digit.
QRegExp("[0-9]{13}") // matches exactly 13 digits (e.g. perhaps for ISBN?).
QRegExp("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
// matches a format similar to an ip address.
// N.B. invalid addresses can still be entered: "999.999.999.999".
More On Line-edit Behaviour
更多在线编辑行为
According to documentation:
根据文档:
Note that if there is a validator set on the line edit, the returnPressed()/editingFinished() signals will only be emitted if the validator returns QValidator::Acceptable.
请注意,如果在编辑行上设置了验证器,则仅当验证器返回 QValidator::Acceptable 时才会发出 returnPressed()/editingFinished() 信号。
Thus, the line-edit will allow the user to input digits even if the minimum amount has not yet been reached. For example, even if the user hasn't inputted any text against the regex "[0-9]{3,}"
(which requires at least 3 digits), the line-edit still allows the user to type input to reachthat minimum requirement. However, if the user finishes editing without satsifying the requirement of "at least 3 digits", the input would be invalid; the signals returnPressed()
and editingFinished()
won't be emitted.
因此,即使尚未达到最小数量,行编辑也将允许用户输入数字。例如,即使用户没有针对正则表达式输入任何文本"[0-9]{3,}"
(至少需要 3 位数字),行编辑仍然允许用户输入以达到最低要求。但是,如果用户没有满足“至少3位数”的要求就完成了编辑,则输入无效;信号returnPressed()
,editingFinished()
不会被发射。
If the regex had a maximum-bound (e.g. "[0-1]{,4}"
), then the line-edit will stop any input past 4 characters. Additionally, for character sets (i.e. [0-9]
, [0-1]
, [0-9A-F]
, etc.) the line-edit only allows characters from that particular setto be inputted.
如果正则表达式有一个最大界限(例如"[0-1]{,4}"
),那么行编辑将停止超过 4 个字符的任何输入。此外,对于字符集(即[0-9]
、[0-1]
、[0-9A-F]
等),行编辑仅允许输入来自该特定集的字符。
Note that I've only tested this with Qt 5.11 on a macOS, not on other Qt versions or operating systems. But given Qt's cross-platform schema...
请注意,我仅在 macOS 上使用 Qt 5.11 对此进行了测试,未在其他 Qt 版本或操作系统上进行过测试。但是考虑到 Qt 的跨平台架构......
Demo: Regex Validators Showcase
演示:正则表达式验证器展示
回答by Stefan van den Akker
You could also set an inputMask
:
您还可以设置一个inputMask
:
QLineEdit.setInputMask("9")
This allows the user to type only one digit ranging from 0
to 9
. Use multiple 9
's to allow the user to enter multiple numbers. See also the complete list of characters that can be used in an input mask.
这允许用户只键入从0
到 的一位数字9
。使用 multiple9
来允许用户输入多个数字。另请参阅可在输入掩码中使用的完整字符列表。
(My answer is in Python, but it should not be hard to transform it to C++)
(我的答案是用Python,不过转换成C++应该不难)
回答by p.i.g.
Why don't you use a QSpinBox
for this purpose ? You can set the up/down buttons invisible with the following line of codes:
你为什么不QSpinBox
为此目的使用 a ?您可以使用以下代码行将向上/向下按钮设置为不可见:
// ...
QSpinBox* spinBox = new QSpinBox( this );
spinBox->setButtonSymbols( QAbstractSpinBox::NoButtons ); // After this it looks just like a QLineEdit.
//...
回答by Roberto Sepúlveda Bravo
If you're using QT Creator 5.6 you can do that like this:
如果您使用的是 QT Creator 5.6,您可以这样做:
#include <QIntValidator>
ui->myLineEditName->setValidator( new QIntValidator);
I recomend you put that line after ui->setupUi(this);
我建议你把那行放在 ui->setupUi(this); 之后。
I hope this helps.
我希望这有帮助。