一、創建QtGui應用,項目名稱test,基類"QDialog", 類名保持Dialog,選擇"建立界面"函數
二、雙擊dialog.ui,打開Qt設計器,中間的空白視窗就是ParentWidget,接着須要創建一些ChildWidget。在工具箱中找到須要的Widget:Label,LineEdit[輸入文件],HorizontalSpacer和兩個PushButton[默認1個]。工具
三、設置widget的屬性佈局
四、運行工程·,則界面中的label會顯示&ui
選擇"編輯夥伴Buddsies"命令,在此模式下,能夠設置夥伴this
選擇label並拖到lineEdit,而後放開,這時候會有一個紅色箭頭label->lineEdit。spa
再次運行程序,label中的&再也不出現,此時label和lineEdit這兩個Widget互爲夥伴。選擇"編輯夥伴Buddsies",既可離開此模式,回到本來的編輯模式設計
五、對Widget進行位置編排的佈局LayOut3d
六、單擊編輯[Tab]鍵順序按鈕,每一個widget上都會出現一個方框顯示數字,表示安裝Tab鍵的順序,調整到須要順序,而後回到編輯模式code
運行程序效果此時爲:orm
七、在頭文件"dialog.h"中的dialog類聲明中添加語句
private slots: void on_lineEdit_textChange();
八、在源文件"dialog.cpp"中的構造函數中添加代碼以下:
#include "dialog.h" #include "ui_dialog.h" #include <QRegExp> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); //產生界面以後,setupUi將會根據naming convention對slot進行鏈接,即鏈接on_objectName_signalName()與 //ObectName中的siganlName的signal,也就是自動建立: connect(ui->lineEdit, SIGNAL(textChange(QString)), this, SLOT(on_lineEdit_textChange)); QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}"); //限制字元輸入的範圍:只容許第一個字元輸入大小寫英文字母,後面接一個非0的數字,在接0-2位可爲0的數字 ui->lineEdit->setValidator(new QRegExpValidator(regExp, this)); //設置值的類型 connect(ui->okButton, SIGNAL(clicked()), this, SLOT(accept())); //鏈接OK按鈕到Dialog的accept()槽函數 connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(reject())); //accept和reject槽函數都會關閉Dialog視窗,可是accept會設置Dialog的結果至QDialog::Accepted(結果爲1); //reject會設置Dialog的結果至QDialog::rejected(結果爲1);所以能夠根據這個結果設置按鈕的是ok仍是cancel } Dialog::~Dialog() { delete ui; } void Dialog::on_lineEdit_textChange() { ui->okButton->setEnabled(ui->lineEdit->hasAcceptableInput()); //根據輸入的文字是否有效來啓用或者中止"OK"按鈕 //QLineEdit::hasAcceptableInput()中使用構造函數中的Validator }