Qt::QLineEdit

QLineEdit的继承关系:QLineEdit -> QWidget -> QObject, QPaintDevice,下面笔者通过代码来演示QLineEdit 的一些常见的用法:

/*
    编者:[email protected]

    功能:QLineEdit使用演示

    环境:Win10 Professional 1703 / Qt Creator 4.3.1 Based on Qt 5.9.1 (MSVC 2015, 32 bit) / mingw 5.3.0 32-bit

    备注:代码中只能输入8个数字
*/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QDialog *myQDialog = new QDialog();

    QLabel *myQLabel = new QLabel();
    myQLabel->setText(QObject::tr("行编辑使用示例:"));

    /* 创建行编辑 */
    QLineEdit *myQLineEdit = new QLineEdit();

    /* 输入前先清除 */
    myQLineEdit->clear();

    /* 设置提示字符 */
    myQLineEdit->setPlaceholderText(QObject::tr("this is myQLineEdit"));

    /* 设置大小 */
    myQLineEdit->setMaximumSize(200, 30);

    /* 设置对齐方式 */
    myQLineEdit->setAlignment(Qt::AlignLeft);

    /* 输入时的显示样式,以下是设置为密码的显示样式 */
    myQLineEdit->setEchoMode(QLineEdit::Password);

    /* 设置显示的字体 */
    myQLineEdit->setFont(QFont("Consolas", 9));

    /* 设置是否能编辑 */
    myQLineEdit->setReadOnly(false);

    /* 设置输入字符数限制 */
    myQLineEdit->setMaxLength(13);

    /* 设置只能输入8位的数字密码 */
    myQLineEdit->setValidator(new QRegExpValidator(QRegExp("[0-9]{8}")));

    QHBoxLayout *myQHBoxLayout = new QHBoxLayout();

    myQHBoxLayout->addWidget(myQLabel);

    myQHBoxLayout->addWidget(myQLineEdit);

    myQDialog->setLayout(myQHBoxLayout);

    myQDialog->resize(300, 100);

    myQDialog->show();

    return a.exec();
}

附上运行效果图:
Qt::QLineEdit_第1张图片

你可能感兴趣的:(qt)