久久婷婷香蕉热狠狠综合,精品无码国产自产拍在线观看蜜,寡妇房东在做爰3,中文字幕日本人妻久久久免费,国产成人精品三上悠亚久久

Hi,歡迎來到嵌入式培訓高端品牌 - 華清遠見教育科技集團<北京總部官網>,專注嵌入式工程師培養15年!
當前位置: > 華清遠見教育科技集團 > 嵌入式學習 > 講師博文 > 用兩種方法教你從零創建Qt對話框程序
用兩種方法教你從零創建Qt對話框程序
時(shi)間:2017-01-04作者:華清遠見

本文用兩(liang)種(zhong)方法從(cong)零(ling)教您實現〈C++ GUI Qt4 編程(第二(er)版)〉上的一個經(jing)典案例。

第一(yi)種方(fang)法(fa)是使(shi)用(yong)Qt Creator來(lai)(lai)設計對話(hua)框(kuang)的(de)外觀,然后手工寫代(dai)碼,實現功(gong)能。此方(fang)法(fa),對于初學者來(lai)(lai)說(shuo),能夠很好的(de)理(li)解Qt程序的(de)基(ji)本原(yuan)理(li),為(wei)深入(ru)學習(xi)Qt打下(xia)一(yi)個(ge)很好的(de)基(ji)礎。

第二種方法(fa)是完全(quan)借助于Qt Creator完成(cheng)(cheng)程(cheng)序。此(ci)方法(fa),對于初學(xue)者(zhe)來說,能夠(gou)快速的完成(cheng)(cheng)一個對話框程(cheng)序,從而激發出學(xue)者(zhe)才興趣。

一、 Qt Creator僅用來設計Form外觀

1) 創建窗體(ti)

FileànewàQt Designer FormàWidget 
        選擇路徑 
    &nbsp;   命名為gotocelldialog.ui

2) 創建(jian)子窗口部件

文本標簽:objectName的屬性是”label”,text的屬性是”&Cell Location”
        行編輯器:objectName—lineEdit 
        按鈕: objectName—okButton, enabled—false, text—OK, default—true
        按鈕: objectName—cancelButton, text—Cancel, 
        選中窗體: objectName—GoToCellDialog, windowTitle—Go to Cell
        插入分隔符 
         EditàEdit Buddies,單(dan)擊標簽并把紅色箭頭拖到行(xing)編輯(ji)器中

3) 擺(bai)放窗體部件

選擇label和lineEdit ,單擊FormàLay Out Horizontally
        選擇分隔符、OK按鈕和Cancel按鈕,單擊FormàLay Out Out Horizontally
        單擊窗體中空白處,取消對所有已選中項的選擇,單擊FormàLay Out Vertically
        單(dan)擊(ji)FormàAdjust Size, 重新把窗體的大小定義為(wei)佳形式

4) 設置(zhi)Tab鍵(jian)順(shun)序

可以按(an)照你所希望的接受焦點的順(shun)序(xu),單擊每一(yi)個窗(chuang)口部件,然后點擊Edt-->Edit Widgets,離開Tab鍵順(shun)序(xu)設置(zhi)模式。

5) 在同一目錄下創建(jian)main.cpp,內容如下:

QApplication app(argc, argv);
        Ui::GoToCellDialog ui;
        QDialog *dialog = new QDialog;
        ui.setupUi(dialog);
        dialog->show();
  &nbsp;    &nbsp;return app.exec();

6) 用命(ming)(ming)令行的方(fang)式,執行qmake命(ming)(ming)令,將生(sheng)成pro文(wen)件和(he)Makefile文(wen)件

qmake -project -o gotocelldialog.pro

7) 為對話框(kuang)添加功(gong)能

創建(jian)一個新類,使其同(tong)時從Qdialog和Ui::GoToCellDialog中繼承,命名(ming)慣例是(shi):將該類與(yu)uic所生成(cheng)的類具有(you)(you)相同(tong)的名(ming)字(zi),只是(shi)沒有(you)(you)Ui::前綴而已(yi)。

創建gotocelldialog.h文件
        #ifndef GOTOCELLDIALOG_H
        #define GOTOCELLDIALOG_H 
        #include <QDialog>
        #include "ui_gotocelldialog.h"
        class GoToCellDialog : public QDialog, public Ui::GoToCellDialog 
        {
                Q_OBJECT
        public:
                GoToCellDialog(QWidget *parent = 0);
        private slots:
                void on_lineEdit_textChanged();
        };
    &nbsp;   #endif

創建gotocelldialog.cpp文件 
        include <QtGui>
        #include "gotocelldialog.h”
        GoToCellDialog::GoToCellDialog(QWidget *parent) : QDialog(parent)
        {
                setupUi(this);
                QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
                lineEdit->setValidator(new QRegExpValidator(regExp, this));
                connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
                connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
        }
        void GoToCellDialog::on_lineEdit_textChanged()
        {
                okButton->setEnabled(lineEdit->hasAcceptableInput());
       &nbsp;}

8) 重寫main.cpp

QApplication app(argc, argv);
        GoToCellDialog *dialog = new GoToCellDialog;
        dialog->show();
 &nbsp;      return app.exec();

9) 運行程序 
        ctrl+r
        或者 
        qmake –project –o gotocelldialog.pro
        qmake 
     &nbsp;  ./gotocelldialog

二(er)、直接用Qt Creator 創建Gui工程

1) 建立GUI工(gong)程

●    File-->new-->Qt4 Gui Application
        ●   選擇路徑 
        ●   命名為gotocelldialog.pro
        ●    選擇基類Qdialog, 類名GoToCellDialog,

2)3)4)步驟同前

5) 修(xiu)改gotocelldialog.cpp中的構造(zao)函(han)數(shu)GoToCellDialog

ui->lineEdit->setValidator(new QRegExpValidator(regExp, this));
        connect(ui->okButton, SIGNAL(clicked()), this, SLOT(accept()));
        connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
       &nbsp;備注:此時訪(fang)問部(bu)件,需要通過指針(zhen)ui。

6) 修改gotocelldialog.h,在(zai)類中加入自定義的(de)槽函數原型(xing)

private slots:
&nbsp;       void on_lineEdit_textChanged()

7) 在gotocelldialog.cpp中加入實現

void GoToCellDialog::on_lineEdit_textChanged()
        { 
                ui->okButton->setEnabled(ui->lineEdit->hasAcceptableInput());
&nbsp;       }

備注:本文的(de)部(bu)分內(nei)容參考了(le)〈C++ GUI Qt4 編程(第(di)二版)〉,特此聲(sheng)明,并表示感(gan)謝。

發表評論
評論列表(網友評論僅供網友表達個人看法,并不表明本站同意其觀點或證實其描述)