【问题标题】:QLineEdit accepting only integers and ” and / characters?QLineEdit 只接受整数和 ” 和 / 字符?
【发布时间】:2013-01-09 08:28:33
【问题描述】:

我有一个QLineEdit,我将它用于测量转换应用程序。在那个QLineEdit 中我只能使用整数值,所以我使用了QDoubleValidator

q_LineEdit->setValidator(new QDoubleValidator(this));

现在我希望 QLineEdit 只接受 " 和 / 字符以及整数,因为它是转换应用程序所必需的。如何在使用 @987654327 时让我的 QLineEdit 接受它@?

注意:我希望我的QLineEdit 接受这样的内容(例如70“1/2)。

注意:QLineEdit 不应接受除 " 和 / 之外的任何其他字符。

【问题讨论】:

    标签: qt qt4 qlineedit qregexp


    【解决方案1】:

    最后我自己想出了答案。这很简单。只需使用QRegExpValidator。 这是我的一段代码:

    QRegExp rx("(|\"|/|\\.|[0-9]){30}");
    m_LineEdit->setValidator(new QRegExpValidator(rx, this));
    

    【讨论】:

    • 您还可以使用 QRegExp 或 QRegularExpression 来精确修复您的 QLabel 中接受或不接受的内容。您提供的正则表达式允许 '"""70""1/21/5"12"/51/"5"65//' 作为有效值 ;) QRegExp rx("[0-9]{2}\"[0-9]\/[0-9]"); 可能更好,您只需要修复多少您在每个步骤中接受的数字
    猜你喜欢
    • 1970-01-01
    • 2016-03-27
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2016-02-03
    相关资源
    最近更新 更多