Qt之QLineEdit

来源:互联网 发布:excel数据递增 编辑:程序博客网 时间:2024/05/17 07:55

QLineEdit
setMode() //设置文本编辑显示模式
enum EchoMode:
Normal //普通模式
NoEcho //不允许输入
Password //编辑及显示时为密码样式
PasswordEchoOnEdit //显示时为密码样式
setInputMask() // 设置输入掩码来限制输入字符
setValidator() // 为其设置验证器(validator)来验证输入
setCompleter() // 为其设置自动完成器

#include "widget.h"#include <QLabel>#include <QLineEdit>#include <QHBoxLayout>#include <QVBoxLayout>#include <QValidator>#include <QRegExp>#include <QCompleter>// QLineEdit// setMode() //设置文本编辑显示模式// enum EchoMode:// Normal //普通模式// NoEcho //不允许输入// Password //编辑及显示时为密码样式// PasswordEchoOnEdit //显示时为密码样式// setInputMask() // 设置输入掩码来限制输入字符// setValidator() // 为其设置验证器(validator)来验证输入// setCompleter() // 为其设置自动完成器QHBoxLayout* getlayout(QWidget*,QWidget*);Widget::Widget(QWidget *parent)    : QWidget(parent){    resize(300,300);    QLabel *la1 = new QLabel("显示模式:");    QLineEdit *ld1 = new QLineEdit;    QLabel *la2 = new QLabel("输入掩码:");    QLineEdit *ld2 = new QLineEdit;    QLabel *la3 = new QLabel("输入验证:");    QLineEdit *ld3 = new QLineEdit;    QLabel *la4 = new QLabel("自动完成:");    QLineEdit *ld4 = new QLineEdit;    QHBoxLayout *ret1 = getlayout(la1,ld1);    QHBoxLayout *ret2 = getlayout(la2,ld2);    QHBoxLayout *ret3 = getlayout(la3,ld3);    QHBoxLayout *ret4 = getlayout(la4,ld4);    QVBoxLayout *mainlayout = new QVBoxLayout;    mainlayout->addLayout(ret1);    mainlayout->addLayout(ret2);    mainlayout->addLayout(ret3);    mainlayout->addLayout(ret4);    setLayout(mainlayout);    //显示模式    ld1->setEchoMode(QLineEdit::PasswordEchoOnEdit);    //输入掩码    ld2->setInputMask("D99999999D");//输入前后不能为0的十位数    //输入验证    QRegExp rgx("\\d{11}");//输入十一位数 运用正则表达式    QValidator *validator = new QRegExpValidator(rgx,this);    ld3->setValidator(validator);    //自动验证    QStringList words;    words << "math" << "macro" << "monther";    QCompleter *completer = new QCompleter(words,this);    completer->setCaseSensitivity(Qt::CaseInsensitive);//大小写不敏感    ld4->setCompleter(completer);}QHBoxLayout* getlayout(QWidget* w1,QWidget *w2){    QHBoxLayout *ret = new QHBoxLayout;    ret->addStretch();    ret->addWidget(w1);    ret->addSpacing(10);    ret->addWidget(w2);    ret->addStretch();    return ret;}