此處使用UI Designer直接創(chuàng)建一個QLineEdit
//獲取單行文本框控件指針
QLineEdit *ie=ui->lineEdit;
設(shè)置與獲取內(nèi)容
//設(shè)置文本
ie->setText("this");
//獲取文本
qDebug()<<ie->text();
設(shè)為不可見字符(一般用于密碼)
ie->setEchoMode(QLineEdit::Password);
設(shè)置顯示間隔
如下代碼,四個參數(shù)分別為:
- 左邊距
- 上邊距
- 右邊距
- 下邊距
ie->setTextMargins(15,0,0,0);
以上代碼效果如圖,

image.png
且控制臺正常輸出,

image.png
文本補(bǔ)全提示
首先定義一個QStringList類創(chuàng)建的對象
QStringList list;
list<<"Hello"<<"Hi"<<"Are"<<"you"<<"ok";
定義一個自動補(bǔ)全器并令文本框使用這個補(bǔ)全器
//定義一個自動補(bǔ)全器
QCompleter *com=new QCompleter(list,this);
//令其大小寫不敏感
com->setCaseSensitivity(Qt::CaseInsensitive);
//指定文本框的自動補(bǔ)全器
ie->setCompleter(com);
如圖,此時文本框?qū)⒅С肿詣友a(bǔ)全

image.png