计算器界面分析及界面程序实现
生活随笔
收集整理的這篇文章主要介紹了
计算器界面分析及界面程序实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 1 計算器程序界面分析
- 2 計算器界面程序?qū)崿F(xiàn)
- 2.1 計算器界面程序初步實現(xiàn)
- 2.2 計算器界面代碼重構(gòu)
1 計算器程序界面分析
程序界面如下:
界面設(shè)計:
- 定義組件間的間隔:
- Space = 10px
- 定義按鈕組件的大小:
- Width = 40px,Height = 40px
- 定義文本框組件的大小:
- Width = 5 * 40px + 4 * 10px, Height = 30px
對于Qt應(yīng)用程序開發(fā):
- GUI應(yīng)用程序開發(fā)前必須進行界面設(shè)計。
- GUI應(yīng)用程序界面需要考慮各個細節(jié):
- 界面決定最終用戶的體驗。
- 界面細節(jié)是GUI應(yīng)用程序品質(zhì)的重要體現(xiàn)。
- Qt庫有能力實現(xiàn)各種GUI應(yīng)用程序需求。
- Qt幫助文檔的使用對于開發(fā)是非常重要的。
2 計算器界面程序?qū)崿F(xiàn)
2.1 計算器界面程序初步實現(xiàn)
初步實現(xiàn)的代碼如下:
#include <QtGui/QApplication> #include <QWidget> #include <QLineEdit> #include <QPushButton>int main(int argc, char *argv[]) {QApplication a(argc, argv);QWidget* w = new QWidget(NULL, Qt::WindowCloseButtonHint);QLineEdit* le = new QLineEdit(w);QPushButton* button[20] = {0};const char* btnText[20] ={"7", "8", "9", "+", "(","4", "5", "6", "-", ")","1", "2", "3", "*", "<-","0", ".", "=", "/", "C",};int ret = 0;le->move(10, 10);le->resize(240, 30);le->setReadOnly(true);for(int i=0; i<4; i++){for(int j=0; j<5; j++){button[i*5 + j] = new QPushButton(w);button[i*5 + j]->resize(40, 40);button[i*5 + j]->move(10 + (10 + 40)*j, 50 + (10 + 40)*i);button[i*5 + j]->setText(btnText[i*5 + j]);}}w->show();w->setFixedSize(w->width(), w->height());ret = a.exec();delete w;return ret; }運行效果如下:
2.2 計算器界面代碼重構(gòu)
在重構(gòu)前先了解下重構(gòu)的基本概念,什么是軟件開發(fā)過程中的重構(gòu)呢?
重構(gòu)(Refactoring)是以改善代碼質(zhì)量為目的的代碼重寫:
- 使其軟件的設(shè)計與架構(gòu)更加合理。
- 提高軟件的擴展性和維護性。
代碼實現(xiàn)與代碼重構(gòu)不同:
- 代碼實現(xiàn):
- 按照設(shè)計編程實現(xiàn),重心在于功能實現(xiàn)。
- 代碼重構(gòu):
- 以提高代碼質(zhì)量為目的的軟件架構(gòu)優(yōu)化。
區(qū)別:
- 代碼實現(xiàn)時不考慮架構(gòu)的好壞,只考慮功能的實現(xiàn)。
- 代碼重構(gòu)時不能影響已實現(xiàn)的功能,只考慮架構(gòu)的改善。
下面簡單看一下軟件開發(fā)過程:
- 從工程的角度對軟件開發(fā)中的活動進行定義和管理。
再來看一下什么樣的代碼需要重構(gòu): - 當(dāng)發(fā)現(xiàn)項目中重復(fù)的代碼越來越多時。
- 當(dāng)發(fā)現(xiàn)項目中代碼功能越來越不清晰時。
- 當(dāng)發(fā)現(xiàn)項目中代碼離設(shè)計越來越遠時。
- …
重構(gòu)的進行:
- 重構(gòu)是維持代碼質(zhì)量在可接受范圍內(nèi)的重要方式。
- 重構(gòu)的時機和方式由項目組使用的軟件開發(fā)過程(Process)決定。
計算器界面代碼重構(gòu):
目錄結(jié)構(gòu)如下:
QCalculatorUI.h:
QCalculatorUI.cpp:
#include "QCalculatorUI.h"QCalculatorUI::QCalculatorUI() : QWidget(NULL, Qt::WindowCloseButtonHint) {}bool QCalculatorUI::construct() {bool ret = true;const char* btnText[20] ={"7", "8", "9", "+", "(","4", "5", "6", "-", ")","1", "2", "3", "*", "<-","0", ".", "=", "/", "C",};m_edit = new QLineEdit(this);if( m_edit != NULL ){m_edit->move(10, 10);m_edit->resize(240, 30);m_edit->setReadOnly(true);}else{ret = false;}for(int i=0; (i<4) && ret; i++){for(int j=0; (j<5) && ret; j++){m_buttons[i*5 + j] = new QPushButton(this);if( m_buttons[i*5 + j] != NULL ){m_buttons[i*5 + j]->resize(40, 40);m_buttons[i*5 + j]->move(10 + (10 + 40)*j, 50 + (10 + 40)*i);m_buttons[i*5 + j]->setText(btnText[i*5 + j]);}else{ret = false;}}}return ret; }QCalculatorUI* QCalculatorUI::NewInstance() {QCalculatorUI* ret = new QCalculatorUI();if( (ret == NULL) || !ret->construct() ){delete ret;ret = NULL;}return ret; }void QCalculatorUI::show() {QWidget::show();setFixedSize(width(), height()); }QCalculatorUI::~QCalculatorUI() {}main.cpp:
#include <QtGui/QApplication> #include "QCalculatorUI.h"int main(int argc, char *argv[]) {QApplication a(argc, argv);QCalculatorUI* cal = QCalculatorUI::NewInstance();int ret = -1;if( cal != NULL ){cal->show();ret = a.exec();delete cal;}return ret; }注意:上述代碼中使用了二階構(gòu)造模式,在Qt中如果動態(tài)內(nèi)存申請失敗的話會拋出bad_alloc異常,所以不能使用NULL對返回值進行判斷,這樣做是徒勞的。我們需要使用try catch進行異常捕獲,如果拋出了異常再返回NULL。
參考資料:
總結(jié)
以上是生活随笔為你收集整理的计算器界面分析及界面程序实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。