将QLineEdit设置为仅接受数字

问题描述 投票:69回答:6

我有一个QLineEdit,用户只能输入数字。

那么QLineEdit只有数字设置吗?

c++ qt qlineedit
6个回答
110
投票

QLineEdit::setValidator(),例如:

myLineEdit->setValidator( new QIntValidator(0, 100, this) );

要么

myLineEdit->setValidator( new QDoubleValidator(0, 100, 2, this) );

见:QIntValidatorQDoubleValidatorQLineEdit::setValidator


21
投票

最好的是QSpinBox

而对于双倍价值使用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)));

7
投票

你也可以设置一个inputMask

QLineEdit.setInputMask("9")

这允许用户只键入一个从09的数字。使用多个9允许用户输入多个数字。另见完整的list of characters that can be used in an input mask

(我的答案是在Python中,但将其转换为C ++应该不难)


7
投票

你为什么不为此目的使用QSpinBox?您可以使用以下代码行设置不可见的向上/向下按钮:

// ...
QSpinBox* spinBox = new QSpinBox( this );
spinBox->setButtonSymbols( QAbstractSpinBox::NoButtons ); // After this it looks just like a QLineEdit.
//...

3
投票

正则表达式验证器

到目前为止,其他答案仅为相对有限数量的数字提供解决方案。但是,如果您关注任意或可变数字的数字,您可以使用QRegExpValidator,传递仅接受数字的正则表达式(如user2962533's comment所述)。这是一个简单,完整的例子:

#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();
}

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".     

更多关于行编辑行为

documentation说:

请注意,如果在行编辑中设置了验证器,则只有在验证器返回QValidator :: Acceptable时才会发出returnPressed()/ editingFinished()信号。

因此,即使尚未达到最小量,行编辑也将允许用户输入数字。例如,即使用户没有针对正则表达式"[0-9]{3,}"(需要至少3位数)输入任何文本,行编辑仍然允许用户键入输入以达到该最低要求。但是,如果用户在没有满足“至少3位数”要求的情况下完成编辑,则输入无效;信号returnPressed()editingFinished()将不会被发射。

如果正则表达式具有最大边界(例如"[0-1]{,4}"),则行编辑将停止任何输入超过4个字符。另外,对于字符集(即[0-9][0-1][0-9A-F]等),行编辑仅允许输入来自该特定集的字符。

请注意,我只在Mac OS上使用Qt 5.11进行了测试,而不是在其他Qt版本或操作系统上进行测试。但考虑到Qt的跨平台架构......

但是:Kua zxsw指出


-1
投票

如果你正在使用QT Creator 5.6,你可以这样做:

Regex Validators Showcase

我建议你把它放在ui-> setupUi(this)之后;

我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.