【问题标题】:Set QLineEdit to accept only numbers将 QLineEdit 设置为仅接受数字
【发布时间】:2012-11-05 13:21:22
【问题描述】:

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

那么QLineEdit 是否有仅限数字的设置?

【问题讨论】:

  • QSpinBox 已回答。

标签: c++ qt qlineedit


【解决方案1】:

正则表达式验证器

到目前为止,其他答案仅提供了相对有限位数的解决方案。但是,如果您关心 任意 或 变量 位数,您可以使用 QRegExpValidator,传递一个只接受数字的正则表达式(如 @ 所述987654322@)。这是一个最小的完整示例:

#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] 等),行编辑仅允许输入来自该特定集的字符。

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

演示:Regex Validators Showcase

【讨论】:

    【解决方案2】:

    QLineEdit::setValidator(),例如:

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

    或

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

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

    【讨论】:

    • 这可以从 Qt Designer 中完成,还是只能通过代码完成?
    • 据我所知,设计师没有办法做到这一点。
    • 如果您需要以科学记数法给出的输入(例如,3.14e-7),这是一个快速的解决方案。 QDoubleSpinBox 不接受科学计数法 (Qt 5.5) 中的数字。
    • 如果我输入 (1,100),它仍然会接受 0 作为输入。另外,我可以无限写0(不止3次)!!
    • 另见 QRegExpValidator 与 QRegExp("[0-9]*")。
    【解决方案3】:

    如果您使用的是 QT Creator 5.6,您可以这样做:

    #include <QIntValidator>
    
    ui->myLineEditName->setValidator( new QIntValidator);
    

    我建议你把那一行放在 ui->setupUi(this);

    我希望这会有所帮助。

    【讨论】:

    • 您的构造函数调用应该是new QIntValidator(this),否则一旦您的小部件超出范围,验证器对象就会泄漏。
    【解决方案4】:

    最好的是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)));
    

    【讨论】:

    • 即使 OP 想要使用 QLineEdit,使用 QSpinBox 绝对是最好的方法。
    • 这适用于数字范围较小的情况。考虑一下您可能希望将此小部件用于年龄或 id。
    • 有什么方法可以让 spinbox 对键盘更加友好,只使用数字键、小数分隔符和退格键?
    【解决方案5】:

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

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

    【讨论】:

    • 请注意,NoButons 实际上并没有删除按钮,它只是使它们与 QSpinBox 的背景颜色相同。因此,在某些箭头较大的样式中,QSpinBox 的大部分会令人讨厌,看起来只是空白并挤压输入字段的其余部分
    【解决方案6】:

    你也可以设置inputMask:

    QLineEdit.setInputMask("9")
    

    这允许用户只输入一个数字,范围从0 到9。使用多个9 允许用户输入多个数字。另请参阅完整的list of characters that can be used in an input mask。

    (我的答案是用Python,但转换成C++应该不难)

    【讨论】:

      猜你喜欢
      • 2020-05-17
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      • 2016-03-27
      • 1970-01-01
      • 2019-05-27
      相关资源
      最近更新 更多