首页 文章

如何在Qt中为QLineEdit使用setValidator()和setInputMask()?

提问于
浏览
1

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

我通过QRegexp看了一些例子但是我没有像输入掩码那样的“:” . 谁能指出我如何实施?

2 回答

  • -1

    您可以解决的一种方法是为您的QLineEdit安装installFilter . 然后在eventFilter()中捕获事件并通过QValidator或Qregex验证给定的输入 . 根据验证接受或拒绝 .

    我希望这将有所帮助 .

  • 0

    虽然Qt Docs明确表示如果我们想要将范围控制在 QLineEdits 中,我们将"use masks together with validators",这似乎并非那么微不足道 . 至少其他人有类似的问题 . 但mybe这有助于:

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

    它似乎有点超过顶部,但实际上并没有那么多新代码,你不需要键盘上的冒号键 .

相关问题