2011-07-20 77 views
0

我想在QT4中創建一個簡單的計算器。用戶輸入2個數字,並有4個命令按鈕進行計算。 (+ - * /)。我的代碼是:QT4中的C++簡單計算器

calculator.h

#ifndef CALCULATOR_H 
#define CALCULATOR_H 

#include QWidget 
#include QGridLayout 
#include QLineEdit 
#include QLabel 
#include QPushButton 
#include QLCDNumber 
#include QString 
#include QMessageBox 
#include QErrorMessage 

class Calculator : public QWidget { 

Q_OBJECT 

public: 

//constructor 

Calculator(); 

public slots: 

//function to add the two numbers that the user inputs 

void addition(); 

//function to subtract the two numbers that the user inputs 

void subtraction(); 

//function to multiply the two numbers that the user inputs 

void multiply(); 

//function to divide the two numbers that the user inputs 

void division(); 

//function to clear widgets 

void clearFields(); 

//function to close window 

void close(); 

private: 

QLineEdit* int1Entry; 
QLineEdit* int2Entry; 
QLCDNumber* lineoutput; 
QErrorMessage* error; 

}; 

#endif 

calculator.cpp

#include "Calculator.h" 

//constructor 

Calculator::Calculator() { 

//{initializes and places widgets using a layout 

    setWindowTitle("Simple Calculator"); 
    QGridLayout* layout = new QGridLayout; 
    QPushButton* additionButton = new QPushButton("+"); 
    QPushButton* subtractionButton = new QPushButton("-"); 
    QPushButton* multiplicationButton = new QPushButton("*"); 
    QPushButton* divisionButton = new QPushButton("/"); 

    lineoutput = new QLCDNumber; 

    //result->setBinMode(); 

    lineoutput->setSegmentStyle(QLCDNumber::Flat); 
    lineoutput->setDigitCount(8); 
    QPushButton* clear = new QPushButton("Clear"); 
    QPushButton* close = new QPushButton("Close"); 
    layout->addWidget(int1Entry, 2,1); 
    layout->addWidget(int2Entry, 3,1); 
    layout->addWidget(additionButton, 0,2); 
    layout->addWidget(subtractionButton, 1,2); 
    layout->addWidget(multiplicationButton, 2,2); 
    layout->addWidget(divisionButton, 3,2); 
    layout->addWidget(new QLabel, 1, 0); //for spacing 
    layout->addWidget(lineoutput,3,0); 
    layout->addWidget(close, 4,1); 
    layout->addWidget(clear, 4,2); 
    setLayout(layout); 

    //} 

    //{connect signals and slots 

    connect(additionButton, SIGNAL(clicked()), this, SLOT(addition())); 
    connect(subtractionButton, SIGNAL(clicked()), this, SLOT(subtraction())); 
    connect(multiplicationButton, SIGNAL(clicked()), this, SLOT(multiply())); 
    connect(divisionButton, SIGNAL(clicked()), this, SLOT(division())); 
    connect(clear, SIGNAL(clicked()), this, SLOT(clearFields())); 
    connect(close, SIGNAL(clicked()), this, SLOT(close())); 

    //} 

} 

//function to add the two numbers 

void Calculator::addition() 
{ 

    QString strFisrt = int1Entry->text(); 
    QString strSecond = int2Entry->text(); 
    bool ok; 

    int number1 = strFisrt.toInt(&ok); 
    int number2 = strSecond.toInt(&ok); 
    int result = number1 + number2; 

    QString strResult; 

    strResult = strResult.number(result); 
    lineoutput->display(strResult); 
} 

//function to subtract the two numbers 

void Calculator::subtraction() 

{ 
     QString strFisrt = int1Entry->text(); 
     QString strSecond = int2Entry->text(); 
     bool ok; 

     int number1 = strFisrt.toInt(&ok); 
     int number2 = strSecond.toInt(&ok); 
     int result = number1 - number2; 
     QString strResult; 
     strResult = strResult.number(result); 
     lineoutput->display(strResult); 
} 

//function to multiply the two numbers 

void Calculator::multiply() 

{ 
    QString strFisrt = int1Entry->text(); 
    QString strSecond = int2Entry->text(); 

     bool ok; 

     int number1 = strFisrt.toInt(&ok); 
     int number2 = strSecond.toInt(&ok); 
     int result = number1 * number2; 

     QString strResult; 

     strResult = strResult.number(result); 
     lineoutput->display(strResult); 
} 

//function to divide the two numbers 

void Calculator::division() 

{ 
    QString strFisrt = int1Entry->text(); 
    QString strSecond = int2Entry->text(); 

     bool ok; 

     int number1 = strFisrt.toInt(&ok); 
     int number2 = strSecond.toInt(&ok); 
     int result = number1/number2; 

     QString strResult; 
     strResult = strResult.number(result); 
     lineoutput->display(strResult); 
} 

//clear widgets 

void Calculator::clearFields(){ 

    int1Entry->clear(); 
    int2Entry->clear(); 
    lineoutput->display(0); 
} 

//close window 

void Calculator::close(){ 

    exit(0); 
} 

的main.cpp

#include <QApplication> 
#include "calculator.h" 

int main(int argc, char* argv[]) 

{ 

    QApplication app(argc, argv); 
    Calculator w; 
    w.show(); 
    return app.exec(); 
} 

當我運行這個程序,我得到了一個空白的MS-DOS黑色屏幕。你能幫我嗎?

+3

沒有幫助,直到你以可讀的方式格式化你的答案:P –

+0

我該怎麼做? –

回答

3

的問題是,你永遠不會創建以下項目的實例:

QLineEdit* int1Entry; 
QLineEdit* int2Entry; 

以下內容添加到您的構造函數的開頭:

int1Entry = new QLineEdit; 
int2Entry = new QLineEdit; 

你永遠的QErrorMessage一個實例分配給error但是你從不使用它,所以稍後留意。

+0

非常感謝你 –

+0

我不認爲你把所有的東西都放到了你的評論中,它不完整,所以我不完全確定你在問什麼。 –

+0

我添加了這個輸出一個錯誤消息除以零之後:void Calculator :: division() { QString strFisrt = int1Entry-> text(); QString strSecond = int2Entry-> text(); bool ok; ! INT I = strSecond.toInt(&ok); 如果(OK){ 誤差=新QErrorMessage(本); 無錯誤> showMessage() 「由零不允許司」; } INT NUMBER1 = strFisrt.toInt( &ok); INT NUMBER2 = strSecond.toInt(&ok); INT結果= NUM​​BER1 /數字2; QString的strResult; strResult = strResult.number(結果); lineoutput->顯示(ⅰ); } –