本文用兩種方法從零教您實現(xiàn)〈C++ GUI Qt4 編程(第二版)〉上的一個經(jīng)典案例。
第一種方法是使用Qt Creator來設(shè)計對話框的外觀,然后手工寫代碼,實現(xiàn)功能。此方法,對于初學(xué)者來說,能夠很好的理解Qt程序的基本原理,為深入學(xué)習(xí)Qt打下一個很好的基礎(chǔ)。
第二種方法是完全借助于Qt Creator完成程序。此方法,對于初學(xué)者來說,能夠快速的完成一個對話框程序,從而激發(fā)出學(xué)者才興趣。
一、 Qt Creator僅用來設(shè)計Form外觀
1) 創(chuàng)建窗體
FileànewàQt Designer FormàWidget
選擇路徑
命名為gotocelldialog.ui
2) 創(chuàng)建子窗口部件
文本標(biāo)簽: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,單擊標(biāo)簽并把紅色箭頭拖到行編輯器中
3) 擺放窗體部件
選擇label和lineEdit ,單擊FormàLay Out Horizontally
選擇分隔符、OK按鈕和Cancel按鈕,單擊FormàLay Out Out Horizontally
單擊窗體中空白處,取消對所有已選中項的選擇,單擊FormàLay Out Vertically
單擊FormàAdjust Size, 重新把窗體的大小定義為佳形式
4) 設(shè)置Tab鍵順序
可以按照你所希望的接受焦點的順序,單擊每一個窗口部件,然后點擊Edt-->Edit Widgets,離開Tab鍵順序設(shè)置模式。
5) 在同一目錄下創(chuàng)建main.cpp,內(nèi)容如下:
QApplication app(argc, argv);
Ui::GoToCellDialog ui;
QDialog *dialog = new QDialog;
ui.setupUi(dialog);
dialog->show();
return app.exec();
6) 用命令行的方式,執(zhí)行qmake命令,將生成pro文件和Makefile文件
qmake -project -o gotocelldialog.pro
7) 為對話框添加功能
創(chuàng)建一個新類,使其同時從Qdialog和Ui::GoToCellDialog中繼承,命名慣例是:將該類與uic所生成的類具有相同的名字,只是沒有Ui::前綴而已。
創(chuàng)建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();
};
#endif
創(chuàng)建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());
}
8) 重寫main.cpp
QApplication app(argc, argv);
GoToCellDialog *dialog = new GoToCellDialog;
dialog->show();
return app.exec();
9) 運行程序
ctrl+r
或者
qmake –project –o gotocelldialog.pro
qmake
./gotocelldialog
二、直接用Qt Creator 創(chuàng)建Gui工程
1) 建立GUI工程
● File-->new-->Qt4 Gui Application
● 選擇路徑
● 命名為gotocelldialog.pro
● 選擇基類Qdialog, 類名GoToCellDialog,
2)3)4)步驟同前
5) 修改gotocelldialog.cpp中的構(gòu)造函數(shù)GoToCellDialog
ui->lineEdit->setValidator(new QRegExpValidator(regExp, this));
connect(ui->okButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
備注:此時訪問部件,需要通過指針ui。
6) 修改gotocelldialog.h,在類中加入自定義的槽函數(shù)原型
private slots:
void on_lineEdit_textChanged()
7) 在gotocelldialog.cpp中加入實現(xiàn)
void GoToCellDialog::on_lineEdit_textChanged()
{
ui->okButton->setEnabled(ui->lineEdit->hasAcceptableInput());
}
備注:本文的部分內(nèi)容參考了〈C++ GUI Qt4 編程(第二版)〉,特此聲明,并表示感謝。