C++ QT4中的简单计算器

发布于 2024-11-25 02:44:10 字数 4841 浏览 0 评论 0原文

我正在尝试在 QT4 中创建一个简单的计算器。用户输入 2 个数字,有 4 个命令按钮进行计算。 (+ - * /)。我的代码是:

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

计算器.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黑屏。你能帮我吗?

I am trying to create a simple calculator in QT4. The user inputs 2 numbers and there are 4 command buttons that do calculations. (+ - * /). My code is:

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();
}

When I run this program I get a blank Ms-Dos black screen. Can u help me please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殊姿 2024-12-02 02:44:10

问题是您从未创建以下项目的实例:

QLineEdit* int1Entry;
QLineEdit* int2Entry;

将以下内容添加到构造函数的开头:

int1Entry = new QLineEdit;
int2Entry = new QLineEdit;

您也从未将 QErrorMessage 实例分配给 error 但您也从未使用它,所以以后要小心。

The problem is that you never create instances of the following items:

QLineEdit* int1Entry;
QLineEdit* int2Entry;

Add the following to the beginning of your constructor:

int1Entry = new QLineEdit;
int2Entry = new QLineEdit;

You never assign an instance of QErrorMessage to error either but you never use it, so watch out for it later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文