【问题标题】:How to use both setValidator() and setInputMask() for QLineEdit in Qt?如何在 Qt 中同时使用 setValidator() 和 setInputMask() 进行 QLineEdit?
【发布时间】:2016-03-30 11:03:57
【问题描述】:

我有一个行编辑,用户必须以 12 小时时间格式输入。这是一个基于触摸的应用程序,我有自己的键盘,而这个键盘没有 ":"(冒号)字符。因此,我为此目的使用了输入掩码。但是我只有setInputMask( 99:99 ) 选项,它允许用户输入任何数字,而我必须将用户限制为 12 小时格式。

我浏览了一些使用 QRegexp 的示例,但我无法在输入掩码中使用“:”。谁能指点我如何实施?

【问题讨论】:

  • 用QTimeEdit怎么样?
  • @hank 不,我不能在这里使用它。要求:)

标签: qt qlineedit qregexp input-mask


【解决方案1】:

虽然 Qt Docs 明确表示如果我们想要在QLineEdits 之外进行范围控制,我们将“将掩码与验证器一起使用”,但这似乎并不是那么微不足道。至少其他人也有类似的问题。但这有帮助:

子类化QLineEdit 并覆盖focusInEvent() 和setValidator(),如下所示:

----------------- mylineedit.h ----------

#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H

#include <QLineEdit>
#include <QFocusEvent>
#include <QRegExpValidator>

class MyLineEdit : public QLineEdit
{
    Q_OBJECT

private:

    const QValidator *validator;

public:

    MyLineEdit(QWidget *parent = 0);

    void setValidator(const QValidator *v);

protected:

    void focusInEvent(QFocusEvent *e);
};

#endif // MYLINEEDIT_H

------------------- mylineedit.cpp -------------------

#include "mylineedit.h"

MyLineEdit::MyLineEdit(QWidget *parent): QLineEdit(parent)
{

}

void MyLineEdit::setValidator(const QValidator *v)
{
    validator = v;
    QLineEdit::setValidator(v);
}

void MyLineEdit::focusInEvent(QFocusEvent *e)
{
    Q_UNUSED(e);
    clear();
    setInputMask("");
    setValidator(validator);
}

对于MyLineEdit 对象本身,我们使用QRegExp 来允许此时仅输入不带冒号的12 小时格式的时间:1152 例如。当用户结束编辑 lineedit 时,会得到一个 "HH:HH" 形式的 InpuMask,它使 1152 变为 11:52。焦点被清除。如果用户再次关注 lineedit,则会将其清除并再次设置 QRegExp,并且用户输入新时间 1245,例如等等……

--------------------- rootwindow.h --------------------

#ifndef ROOTWINDOW_H
#define ROOTWINDOW_H

#include "mylineedit.h"
#include <QMainWindow>
#include <QWidget>
#include <QtDebug>

class RootWindow : public QMainWindow
{
    Q_OBJECT

private:

    QWidget *widgetCentral;
    MyLineEdit *line;

public:

    RootWindow(QWidget *parent = 0);
    ~RootWindow();

private slots:

    void slotLineEdited();
};

#endif // ROOTWINDOW_H

------------- rootwindow.cpp ----------

#include "rootwindow.h"

RootWindow::RootWindow(QWidget *parent): QMainWindow(parent)
{
    setCentralWidget(widgetCentral = new QWidget);

    line = new MyLineEdit(widgetCentral);
    line->setValidator(new QRegExpValidator(  QRegExp("[0][0-9][0-5][0-9]|[1][0-2][0-5][0-9]")  ));

    connect(line, SIGNAL(editingFinished()), this, SLOT(slotLineEdited()));
}

RootWindow::~RootWindow()
{

}

void RootWindow::slotLineEdited()
{
    line->setInputMask("HH:HH");
    line->clearFocus();
}

---------- main.cpp ---------- ----

#include "rootwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    RootWindow w;
    w.show();

    return a.exec();
}

这似乎有点过头了,但实际上它并没有那么多新代码,而且您不需要键盘上的冒号键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 2021-08-14
    相关资源
    最近更新 更多