Line Edit
QLineEdit ⽤来表⽰单⾏输⼊框.可以输⼊⼀段⽂本,但是不能换⾏.
| 属性 | 说明 |
|---|---|
| text | 输⼊框中的⽂本 |
| inputMask | 输⼊内容格式约束 |
| maxLength | 最⼤⻓度 |
| frame | 是否添加边框 |
| echoMode | 显⽰⽅式. • QLineEdit::Normal :这是默认值,⽂本框会显⽰输⼊的⽂本。 • QLineEdit::Password :在这种模式下,输⼊的字符会被隐藏,通常⽤星号( *)或等号(=)代替。• QLineEdit::NoEcho :在这种模式下,⽂本框不会显⽰任何输⼊ 的字符。 |
| cursorPosition | 光标所在位置 |
| alignment | ⽂字对⻬⽅式,设置⽔平和垂直⽅向的对⻬. |
| dragEnabled | 是否允许拖拽 |
| readOnly | 是否是只读的(不允许修改) |
| placeHolderText | 当输⼊框内容为空的时候,显⽰什么样的提⽰信息 |
| clearButtonEnabled | 是否会⾃动显⽰出"清除按钮" |
| 属性 | 说明 |
|---|---|
| void cursorPositionChanged(int old, int new) | 当⿏标移动时发出此信号,old为先前的位置,new为新位置。 |
| void editingFinished() | 当按返回或者回⻋键时,或者⾏编辑失去焦点时,发出此信号。 |
| void returnPressed() | 当返回或回⻋键按下时发出此信号. 如果设置了验证器,必须要验证通过,才能触发. |
| void selectionChanged() | 当选中的⽂本改变时,发出此信号。 |
| void textChanged(const QString &text) | 当QLineEdit中的⽂本改变时,发出此信号,text是新的⽂本。 代码对⽂本的修改能够触发这个信号. |
| void textEdited(const QString &text)) | 当QLineEdit中的⽂本改变时,发出此信号,text是新的⽂本。 代码对⽂本的修改不能触发这个信号 |
代码示例:录⼊个⼈信息
让用户输入个人信息~
1)姓名 2)密码 3)性别 4)电话
通过提交按钮,把上述内容给统一获取到~~
1)在界⾯上创建三个输⼊框和两个单选按钮,⼀个普通按钮.
三个输⼊框的 objectName 为 lineEdit_name , lineEdit_password ,lineEdit_phone
两个单选按钮的 objectName 为 radioButton_male , radioButton_female
按钮的 objectName 为 pushButton
![![[Pasted image 20250420191107.png]]](https://i-blog.csdnimg.cn/direct/205ffb339930445485c0b9adeb0c86d1.png)
2)编写widget.cpp,在构造函数中编写初始化代码.
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//编写第一个输入框,用来输入姓名
ui->lineEdit_name->setPlaceholderText("请输入姓名");
ui->lineEdit_name->setClearButtonEnabled(true);
//初始化第二个输入框
ui->lineEdit_password->setPlaceholderText("请输入密码");
ui->lineEdit_password->setClearButtonEnabled(true);
//把显示模式设置成显示密码的模式
ui->lineEdit_password->setEchoMode(QLineEdit::Password);

最低0.47元/天 解锁文章
5117

被折叠的 条评论
为什么被折叠?



