将QLineEdit设置为仅接受数字

sas*_*alm 71 c++ qt qlineedit

我有一个QLineEdit用户应该只输入数字的地方.

那么是否只有数字设置QLineEdit

Chr*_*ris 114

QLineEdit::setValidator(), 例如:

myLineEdit->setValidator( new QIntValidator(0, 100, this) );
Run Code Online (Sandbox Code Playgroud)

要么

myLineEdit->setValidator( new QDoubleValidator(0, 100, 2, this) );
Run Code Online (Sandbox Code Playgroud)

请参阅:QIntValidator,QDoubleValidator,QLineEdit :: setValidator

  • 这可以从Qt Designer完成,还是仅通过代码实现? (4认同)
  • 据我所知,设计师无法做到这一点. (2认同)
  • 另见 [QRegExpValidator](http://doc.qt.io/qt-5/qregexpvalidator.html) 和 QRegExp("[0-9]*")。 (2认同)

Imm*_*lPC 22

最好的是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)));
Run Code Online (Sandbox Code Playgroud)

  • 即使 OP 想要使用 QLineEdit,使用 QSpinBox 绝对是最好的方法。 (2认同)
  • 这在数字范围较小时有效。考虑一下您可能想要使用这个小部件来获取年龄或 id。 (2认同)

Ste*_*ker 7

你也可以设置一个inputMask:

QLineEdit.setInputMask("9")
Run Code Online (Sandbox Code Playgroud)

这允许用户键入只有一个数字范围从09.使用multiple 9允许用户输入多个数字.另请参阅可在输入掩码中使用的完整字符列表.

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


p.i*_*.g. 7

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

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


Tre*_*edJ 5

正则表达式验证器

到目前为止,其他答案仅提供了相对有限数量的数字的解决方案。但是,如果您担心数字的任意可变QRegExpValidator,则可以使用a ,传递仅接受数字的正则表达式(如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();
}
Run Code Online (Sandbox Code Playgroud)

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".     
Run Code Online (Sandbox Code Playgroud)

更多在线编辑行为

根据文件

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

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

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

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

演示:正则表达式验证器展示