加法器

adder.pro

QT += gui
QT += widgets

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += 
        main.cpp 
    myadder.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += 
    adder.h 
    myadder.h 
    myadder.h

FORMS += 
    adder.ui

adder.h

/********************************************************************************
** Form generated from reading UI file 'adder.ui'
**
** Created by: Qt User Interface Compiler version 5.12.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef ADDER_H
#define ADDER_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QDialog>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>

QT_BEGIN_NAMESPACE

class Ui_Dialog
{
public:
    QLineEdit *adder;
    QLineEdit *addered;
    QLineEdit *result;
    QLabel *oper;
    QPushButton *equal;

    void setupUi(QDialog *Dialog)
    {
        if (Dialog->objectName().isEmpty())
            Dialog->setObjectName(QString::fromUtf8("Dialog"));
        Dialog->resize(455, 50);
        adder = new QLineEdit(Dialog);
        adder->setObjectName(QString::fromUtf8("adder"));
        adder->setGeometry(QRect(10, 10, 113, 20));
        addered = new QLineEdit(Dialog);
        addered->setObjectName(QString::fromUtf8("addered"));
        addered->setGeometry(QRect(150, 10, 113, 20));
        result = new QLineEdit(Dialog);
        result->setObjectName(QString::fromUtf8("result"));
        result->setGeometry(QRect(330, 10, 113, 20));
        oper = new QLabel(Dialog);
        oper->setObjectName(QString::fromUtf8("oper"));
        oper->setGeometry(QRect(135, 10, 20, 20));
        equal = new QPushButton(Dialog);
        equal->setObjectName(QString::fromUtf8("equal"));
        equal->setGeometry(QRect(284, 10, 30, 23));

        retranslateUi(Dialog);

        QMetaObject::connectSlotsByName(Dialog);
    } // setupUi

    void retranslateUi(QDialog *Dialog)
    {
        Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", nullptr));
        oper->setText(QApplication::translate("Dialog", "+", nullptr));
        equal->setText(QApplication::translate("Dialog", "=", nullptr));
    } // retranslateUi

};

namespace Ui {
    class Dialog: public Ui_Dialog {};
} // namespace Ui

QT_END_NAMESPACE

#endif // ADDER_H

myadder.h

#ifndef MYADDER_H
#define MYADDER_H

#include <QDialog>
#include "adder.h"

class myadder : public QDialog
{
    Q_OBJECT
    Ui_Dialog* ui;
public:
    myadder();
    ~myadder();
public slots:
    void getRes();
};

#endif // MYADDER_H

myadder.cpp

#include <QDialog>
#include "myadder.h"

myadder::myadder()
{
    ui = new Ui_Dialog();
    ui->setupUi(this);
    connect(ui->equal, SIGNAL(clicked()), this, SLOT(getRes()));
}

myadder::~myadder()
{
    delete ui;
}

void myadder::getRes()
{
    double add = ui->adder->text().toDouble();
    double added = ui->addered->text().toDouble();
    double res = add + added;
    QString strRes = QString::number(res);
    ui->result->setText(strRes);
}

main.cpp

#include <QApplication>
#include "splinbox.h"
#include "myadder.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    myadder adder;
    adder.show();

    return a.exec();
}
原文地址:https://www.cnblogs.com/kuikuitage/p/12820513.html