簡述
QLineEdit是一個單行文本輸入框,帶有撤銷、剪切、粘貼以及拖拽等功能。
通過改變輸入框的echoMode(),同時也可以設置爲一個“只寫”字段,用於輸入密碼等。
文本的長度可以被限制爲maxLength(),可以使用一個validator()或inputMask()來任意限制文本。當在同一個輸入框中切換驗證器和輸入掩碼的時候,最好是清除驗證器或輸入掩碼,防止不確定的行爲。
用法
下面用一個小例子來展示一個QLineEdit的基礎效果
效果
源碼
QLineEdit *lineEdit1 = new QLineEdit;
lineEdit1->setEchoMode(QLineEdit::Normal);
lineEdit1->setPlaceholderText("Normal");
QLineEdit *lineEdit2 = new QLineEdit;
lineEdit2->setEchoMode(QLineEdit::Password);
lineEdit2->setPlaceholderText("Password");
QLineEdit *lineEdit3 = new QLineEdit;
lineEdit3->setEchoMode(QLineEdit::NoEcho);
lineEdit3->setPlaceholderText("NoEcho");
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(lineEdit1);
layout->addWidget(lineEdit2);
layout->addWidget(lineEdit3);
this->setLayout(layout);
常用接口
常用屬性
- 設置輸入掩碼
- inputMask
字符 | ASCII字母字符是必須的,A-Z、a-z。 |
---|---|
A | ASCII字母字符是允許的,但不是必須的。 |
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 | 二進制數據字符是允許的,但不是必須要的。 |
> | 所有的字符字母都大寫。 |
< | 所有的字符字母都小寫。 |
! | 關閉大小寫轉換 |
\ | 使用 \ 去轉義上述列出的字符。 |
掩碼由掩碼字符和分隔符字符串組成,後面可以跟一個分號和用於空白的字符,空白字符在編輯後總是從文本中刪除。
掩碼 | 小提示 |
---|---|
000.000.000.000;_ | IP地址,空白是_ |
HH:HH:HH:HH:HH:HH;_ | MAC地址 |
0000-00-00 | ISO日期,空白是空格。 |
>AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;# | License號,空白是-,並且所有(字母)字符轉換爲大寫。 |
- 文本清除按鈕
- 一個用來清除輸入框中文本的按鈕
- 佔位符文本
- placeHolderText
- 拖拽文本
- dragEnabled
- 選中的文本
- selectedText
- 輸出模式
- echoMode
常量 | 值 | 描述 |
---|---|---|
QLineEdit::Normal | 0 | 正常顯示輸入的字符,默認選項 |
QLineEdit::NoEcho | 1 | 不顯示任何輸入,常用於密碼類型,其密碼長度都需要保密的時候 |
QLineEdit::Password | 2 | 顯示平臺相關的密碼掩碼字符,而不是實際的字符輸入 |
QLineEdit::PasswordEchoOnEdit | 3 | 在編輯的時候顯示字符,負責顯示密碼類型 |
公有函數
- 添加動作
- addAction
效果
源碼
QLineEdit *lineEdit1 = new QLineEdit;
lineEdit1->setEchoMode(QLineEdit::Normal);
lineEdit1->addAction(QIcon(":/picture/google.png"), QLineEdit::LeadingPosition);
lineEdit1->addAction(QIcon(":/picture/search.png"), QLineEdit::TrailingPosition);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(lineEdit1);
this->setLayout(layout);
注:lineEdit1->addAction會返回一個QAction指針,我們可以利用這個指針獲得QAction的信號並與相應槽函數綁定。
- 設置輸入文本對齊方式
- setAlignment
- 設置清除按鈕
- setClearButtonEnabled
效果
源碼
QLineEdit *lineEdit1 = new QLineEdit;
lineEdit1->setClearButtonEnabled(true);
lineEdit1->setCursorMoveStyle(Qt::VisualMoveStyle);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(lineEdit1);
this->setLayout(layout);
- 拖拽文本
- setDragEnabled
效果
源碼
lineEdit1->setDragEnabled(true);
- 設置輸出模式
- setEchoMode
效果
源碼
QLineEdit *lineEdit1 = new QLineEdit;
lineEdit1->setEchoMode(QLineEdit::Normal);
QLineEdit *lineEdit2 = new QLineEdit;
lineEdit2->setEchoMode(QLineEdit::NoEcho);
QLineEdit *lineEdit3 = new QLineEdit;
lineEdit3->setEchoMode(QLineEdit::Password);
QLineEdit *lineEdit4 = new QLineEdit;
lineEdit4->setEchoMode(QLineEdit::PasswordEchoOnEdit);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(lineEdit1);
layout->addWidget(lineEdit2);
layout->addWidget(lineEdit3);
layout->addWidget(lineEdit4);
this->setLayout(layout);
- 設置輸入掩碼
- setInputMask
效果
源碼
QLineEdit *lineEdit1 = new QLineEdit;
lineEdit1->setInputMask("000.000.000.000;_");
QLineEdit *lineEdit2 = new QLineEdit;
lineEdit2->setInputMask("HH:HH:HH:HH:HH:HH;_");
QLineEdit *lineEdit3 = new QLineEdit;
lineEdit3->setInputMask("0000-00-00");
QLineEdit *lineEdit4 = new QLineEdit;
lineEdit4->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#");
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(lineEdit1);
layout->addWidget(lineEdit2);
layout->addWidget(lineEdit3);
layout->addWidget(lineEdit4);
this->setLayout(layout);
- 設置佔位符
- setPlaceHolderText
效果
源碼
QLineEdit *lineEdit1 = new QLineEdit;
lineEdit1->setEchoMode(QLineEdit::Normal);
lineEdit1->setPlaceholderText("Normal");
QLineEdit *lineEdit2 = new QLineEdit;
lineEdit2->setEchoMode(QLineEdit::NoEcho);
lineEdit2->setPlaceholderText("NoEcho");
QLineEdit *lineEdit3 = new QLineEdit;
lineEdit3->setEchoMode(QLineEdit::Password);
lineEdit3->setPlaceholderText("PassWard");
QLineEdit *lineEdit4 = new QLineEdit;
lineEdit4->setEchoMode(QLineEdit::PasswordEchoOnEdit);
lineEdit4->setPlaceholderText("PasswordEchoOnEdit");
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(lineEdit1);
layout->addWidget(lineEdit2);
layout->addWidget(lineEdit3);
layout->addWidget(lineEdit4);
this->setLayout(layout);
槽函數
槽 | 意義 |
---|---|
clear | 清除輸入框中文本 |
copy | 複製選中的文本 |
cut | 剪切選中的文本 |
paste | 將粘貼板中的文本粘貼到輸入框 |
redo | 重複做 |
selectAll | 選中所有文本 |
setText | 爲輸入框設置文本 |
undo | 撤銷 |
信號
信號 | 意義 |
---|---|
cursorPositionChanged | 鼠標位置變化 |
editingFinished | 編輯結束 |
inputRejected | 拒絕輸入 |
returnPressed | 回車鍵按下 |
selectionChanged | 文本選擇變化 |
textChanged | 輸入框中文本變化(編程和用戶輸入) |
texEdited | 輸入框中文本變化(僅限於用戶輸入) |
引用
[1] Qt助手