QLineEdit

QLineEdit

行输入,作用进行输入,有最主要输入类型的控制,此外就是输入格式,即过滤一些东西

部件设置
函数 描述
void setAlignment(Qt::Alignment flag) 对齐方式
void setClearButtonEnabled(bool enable) 是否显示清除全部的按钮
void setCursorMoveStyle(Qt::CursorMoveStyle style) 鼠标形状
void setCursorPosition(int) 鼠标位置
void setDragEnabled(bool b) 允许拖动
void setEchoMode(QLineEdit::EchoMode) 输入类型,password等
void setFrame(bool)
void setInputMask(const QString &inputMask) 控制输入格式
void setMaxLength(int) 长度
void setModified(bool) 可修改
void setPlaceholderText(const QString &) 占位符,没输入时候显示的东西
void setReadOnly(bool) 只读
void setSelection(int start, int length) 设置选择范围
void setTextMargins(int left, int top, int right, int bottom) 设置边框
void setTextMargins(const QMargins &margins)
void setValidator(const QValidator *v) 控制可接受的输入
slots
void    clear()
void    copy() const
void    cut()
void    paste()
void    redo()
void    selectAll()
void    setText(const QString &)
void    undo()
signals
void    cursorPositionChanged(int oldPos, int newPos)
void    editingFinished()
void    returnPressed()
void    selectionChanged()
void    textChanged(const QString &text)
void    textEdited(const QString &text)
其他

1.mask

字符 含义
A ASCII字母字符是必须的,A-Z、a-z。
a ASCII字母字符是允许的,但不是必须的。
N ASCII字母字符是必须的,A-Z、a-z、0-9。
n ASCII字母字符是允许的,但不是必须的。
X 任何字符都是必须要的。
x 任何字符都是允许的,但不是必须要的。
9 ASCII数字是必须要的,0-9。
0 ASCII数字是允许的,但不是必须要的。
D ASCII数字是必须要的,1-9。
d ASCII数字是允许的,但不是必须要的 (1-9)。
# ASCII数字或加/减符号是允许的,但不是必须要的。
H 十六进制数据字符是必须要的,A-F、a-f、0-9。
h 十六进制数据字符是允许的,但不是必须要的。
B 二进制数据字符是必须要的,0-1。
b 二进制数据字符是允许的,但不是必须要的。
> 所有的字符字母都大写
< 所有的字符字母都小写
! 关闭大小写转换

| 使用 \ 去转义上述列出的字符。

例子
/*
 * QLineEdit
 * 输入类型的控制
 * setEchoMode(QLineEdit::Normal);                    
 * setPlaceholderText("Normal");                     
*/
mywidget::mywidget()
{
    setWindowTitle("my widget test");
    QVBoxLayout *layout=new QVBoxLayout(this);
    //echo mode
    QLineEdit *pnormal_line_edit = new QLineEdit(this);
    QLineEdit *pnoecho_line_edit = new QLineEdit(this);
    QLineEdit *ppassword_line_edit = new QLineEdit(this);
    QLineEdit *pdisplay_password_line_edit = new QLineEdit(this);

    pnormal_line_edit->setPlaceholderText("Normal");                        //提示框
    pnoecho_line_edit->setPlaceholderText("NoEcho");
    ppassword_line_edit->setPlaceholderText("Password");
    pdisplay_password_line_edit->setPlaceholderText("PasswordEchoOnEdit");

    pnormal_line_edit->setEchoMode(QLineEdit::Normal);                      //输入方式
    pnoecho_line_edit->setEchoMode(QLineEdit::NoEcho);
    ppassword_line_edit->setEchoMode(QLineEdit::Password);
    pdisplay_password_line_edit->setEchoMode(QLineEdit::PasswordEchoOnEdit);


    layout->addWidget(pnormal_line_edit);
    layout->addWidget(pnoecho_line_edit);
    layout->addWidget(ppassword_line_edit);
    layout->addWidget(pdisplay_password_line_edit);
    this->setFixedSize(300,300);
    this->show();
}
/*
 *AddAction()//添加行为
*/
mywidget::mywidget()
{
    setWindowTitle("my widget test");
    QVBoxLayout *layout=new QVBoxLayout(this);
    //echo mode
    QLineEdit *pline_edit = new QLineEdit(this);
    QAction *pleading_action=new QAction(this);
    pline_edit->setMaxLength(5);                                          //可输入五个字符
    pleading_action->setIcon(QIcon(":/image/wz2.jpg"));
    pline_edit->addAction(pleading_action,QLineEdit::LeadingPosition);
    pline_edit->setPlaceholderText("请输入搜索类容");                        //提示框
    QAction *pTrailingAction = pline_edit->addAction(QIcon(":/image/wz2.jpg"), QLineEdit::TrailingPosition);
    connect(pTrailingAction,QOverload<bool>::of(&QAction::triggered),[&pline_edit]{
        qDebug()<<"进行搜索的内容为:"<<pline_edit->text();});


    layout->addWidget(pline_edit);
    this->setFixedSize(300,300);
    this->show();
}
/*
 * 输入验证
 * QIntValidator整形类表达
 * QDoubleValidator小数类表达
 * QRegExp正则
 * QRegExpValidator正则类型数
 * 掩码
*/
mywidget::mywidget()
{
    setWindowTitle("my widget test");
    QVBoxLayout *layout=new QVBoxLayout(this);
    //input authentication
    QLineEdit *pline_edit = new QLineEdit(this);

    QIntValidator *int_validator=new QIntValidator(this);
    QDoubleValidator *double_validator=new QDoubleValidator(this);
    QRegExpValidator *regexp_validator=new QRegExpValidator(this);
    QRegExp regexp("[a-zA-Z0-9]+$");
    int_validator->setRange(1,99);
    double_validator->setRange(-200,200);
    double_validator->setDecimals(2);
    double_validator->setNotation(QDoubleValidator::StandardNotation);
    regexp_validator->setRegExp(regexp);

//    pline_edit->setValidator(int_validator);
    pline_edit->setValidator(double_validator);
//    pline_edit->setValidator(regexp_validator);

    QLineEdit *pline_edit2=new QLineEdit();
    pline_edit2->setInputMask("000.000.000.000;_");
//    pline_edit2->setInputMask("HH:HH:HH:HH:HH:HH;_");
//    pline_edit2->setInputMask("0000-00-00");
//    pline_edit2->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#");

    layout->addWidget(pline_edit);
    layout->addWidget(pline_edit2);
    this->setFixedSize(300,300);
    this->show();
}

猜你喜欢

转载自blog.csdn.net/qq_33564134/article/details/81531020
今日推荐