這是Qt標準對話框的最後一部分。正如同其名字顯示的同樣,QInputDialog用於接收用戶的輸入。QInputDialog提供了一些簡單的static函數,用於快速的創建一個對話框,正像QColorDialog提供了getColor函數同樣。
首先來看看getText函數:
bool isOK;
![](http://static.javashuo.com/static/loading.gif)
QString text = QInputDialog::getText(NULL,
"Input Dialog",
"Please input your comment",
![](http://static.javashuo.com/static/loading.gif)
QLineEdit::Normal,
"your comment",
![](http://static.javashuo.com/static/loading.gif)
&isOK);
if(isOK) {
![](http://static.javashuo.com/static/loading.gif)
QMessageBox::information(NULL,
"Information",
"Your comment is: <b>" + text +
"</b>",
![](http://static.javashuo.com/static/loading.gif)
QMessageBox::Yes | QMessageBox::No,
![](http://static.javashuo.com/static/loading.gif)
QMessageBox::Yes);
![](http://static.javashuo.com/static/loading.gif)
}
代碼比較簡單,使用getText函數就能夠彈出一個可供用戶輸入的對話框:
下面來看一下這個函數的簽名:
static QString QInputDialog::getText ( QWidget * parent,
const QString & title,
const QString & label,
QLineEdit::EchoMode mode = QLineEdit::Normal,
const QString & text = QString(),
bool * ok = 0,
Qt::WindowFlags flags = 0 )
第一個參數parent,也就是那個熟悉的父組件的指針;第二個參數title就是對話框的標題;第三個參數label是在輸入框上面的提示語句;第四個參數mode用於指明這個QLineEdit的輸入模式,取值範圍是QLineEdit::EchoMode,默認是Normal,也就是正常顯示,你也能夠聲明爲password,這樣就是密碼的輸入顯示了,具體請查閱API;第五個參數text是QLineEdit的默認字符串;第六個參數ok是可選的,若是非NLL,則當用戶按下對話框的OK按鈕時,這個bool變量會被置爲true,能夠由這個去判斷用戶是按下的OK仍是Cancel,從而獲知這個text是否是有意義;第七個參數flags用於指定對話框的樣式。
雖然參數不少,可是每一個參數的含義都比較明顯,你們只要參照API就能夠知道了。
函數的返回值是QString,也就是用戶在QLineEdit裏面輸入的內容。至於這個內容有沒有意義,那就要看那個ok參數是否是true了。
QInputDialog不只提供了獲取字符串的函數,還有getInteger,getDouble,getItem三個相似的函數,這裏就不一一介紹。