Operating system: Tongxin UOSv20
Qt version: 5.11.3
One, a brief introduction
QLineEdit is a single-line text editor that allows users to enter and edit single-line plain text through a series of useful editing functions, including undo and redo, cut and paste, and drag and drop
project | content |
---|---|
Header | QLineEdit |
qmake | QT += widgets |
Inherits | QWidget |
Two, common methods
1. Common methods
method | describe |
---|---|
void setText(const QString &text) | set display text |
QString text() const | get text content |
void setEchoMode(EchoMode) | set output mode |
void setPlaceholderText(const QString &) | Set placeholder prompt |
void setClearButtonEnabled(bool enable) | set clear button |
void setMaxLength(int) | Set the maximum text length |
void setCompleter(QCompleter *completer) | Set up autocompletion |
2. QLineEdit::EchoMode type
type | value | describe |
---|---|---|
QLineEdit::Normal | 0 | Display characters while typing |
QLineEdit::NoEcho | 1 | Do not display any content. This may apply to passwords, even the length of the password should be kept secret |
QLineEdit::Password | 2 | Displays the platform-dependent password mask characters instead of the characters actually entered. |
QLineEdit::PasswordEchoOnEdit | 3 | Display the entered characters when editing, otherwise display the same characters as the password |
Three, use introduction
1. Create objects and basic settings
QLineEdit *le = new QLineEdit(this); //创建QLineEdit对象
le->setGeometry(100, 70, 200, 30); //设置显示位置
le->setText("我是文本输入框"); //设置显示文本
qDebug() << le->text(); //控制台打印文本
2. Display a simple login screen
QLineEdit *leUser = new QLineEdit(this);
leUser->setGeometry(80, 70, 240, 40);
leUser->setEchoMode(QLineEdit::Normal); //设置输出模式为正常模式
leUser->setPlaceholderText("请输入用户名"); //设置占位提示符
leUser->setClearButtonEnabled(true); //设置清空按钮
leUser->setMaxLength(10); //设置文本长度
QLineEdit *lePasswd = new QLineEdit(this);
lePasswd->setGeometry(80, 130, 240, 40);
lePasswd->setEchoMode(QLineEdit::Password); //设置输出模式为密文模式
lePasswd->setPlaceholderText("请输入密码");
lePasswd->setClearButtonEnabled(true);
3. Set up auto-completion
Include the header file QCompleter
QLineEdit *le = new QLineEdit(this); //创建QLineEdit对象
le->setGeometry(100, 70, 200, 30); //设置显示位置
le->setText("我是文本输入框"); //设置显示文本
QStringList list; //创建补全列表
list.append("aaa1"); //列表添加值
list.append("aaa2");
list.append("aaa3");
list.append("bbb1");
QCompleter *completer = new QCompleter(list, le); //创建QCompleter对象
le->setCompleter(completer); //设置自动补全