计算器界面分析及界面程序实现
生活随笔
收集整理的這篇文章主要介紹了
计算器界面分析及界面程序实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1 計算器程序界面分析
- 2 計算器界面程序實現
- 2.1 計算器界面程序初步實現
- 2.2 計算器界面代碼重構
1 計算器程序界面分析
程序界面如下:
界面設計:
- 定義組件間的間隔:
- Space = 10px
- 定義按鈕組件的大小:
- Width = 40px,Height = 40px
- 定義文本框組件的大小:
- Width = 5 * 40px + 4 * 10px, Height = 30px
對于Qt應用程序開發:
- GUI應用程序開發前必須進行界面設計。
- GUI應用程序界面需要考慮各個細節:
- 界面決定最終用戶的體驗。
- 界面細節是GUI應用程序品質的重要體現。
- Qt庫有能力實現各種GUI應用程序需求。
- Qt幫助文檔的使用對于開發是非常重要的。
2 計算器界面程序實現
2.1 計算器界面程序初步實現
初步實現的代碼如下:
#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 計算器界面代碼重構
在重構前先了解下重構的基本概念,什么是軟件開發過程中的重構呢?
重構(Refactoring)是以改善代碼質量為目的的代碼重寫:
- 使其軟件的設計與架構更加合理。
- 提高軟件的擴展性和維護性。
代碼實現與代碼重構不同:
- 代碼實現:
- 按照設計編程實現,重心在于功能實現。
- 代碼重構:
- 以提高代碼質量為目的的軟件架構優化。
區別:
- 代碼實現時不考慮架構的好壞,只考慮功能的實現。
- 代碼重構時不能影響已實現的功能,只考慮架構的改善。
下面簡單看一下軟件開發過程:
- 從工程的角度對軟件開發中的活動進行定義和管理。
再來看一下什么樣的代碼需要重構: - 當發現項目中重復的代碼越來越多時。
- 當發現項目中代碼功能越來越不清晰時。
- 當發現項目中代碼離設計越來越遠時。
- …
重構的進行:
- 重構是維持代碼質量在可接受范圍內的重要方式。
- 重構的時機和方式由項目組使用的軟件開發過程(Process)決定。
計算器界面代碼重構:
目錄結構如下:
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; }注意:上述代碼中使用了二階構造模式,在Qt中如果動態內存申請失敗的話會拋出bad_alloc異常,所以不能使用NULL對返回值進行判斷,這樣做是徒勞的。我們需要使用try catch進行異常捕獲,如果拋出了異常再返回NULL。
參考資料:
總結
以上是生活随笔為你收集整理的计算器界面分析及界面程序实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt中的字符串类
- 下一篇: 计算器初步添加消息响应