Qt--实现简单加法计算器

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include
#include
#include
#include
#include

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
private:
    QPushButton*  btn1;
    QHBoxLayout*  layout1,*layout2;
    QVBoxLayout *layout3;
    QGridLayout  *layout4;
    QLineEdit  *edit1, *edit2, *edit3;
    QLabel *label1;
     QLabel *label2;
private slots:
    void on_clicked();
};

#endif // WIDGET_H
#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    layout1=new QHBoxLayout;
    layout2=new QHBoxLayout;
    //layout3=new QVBoxLayout(this);
    layout4=new QGridLayout(this);

    btn1=new QPushButton;
    edit1=new QLineEdit;
    edit2=new QLineEdit;
    edit3=new QLineEdit;
     label1=new QLabel;

//    layout1->addWidget(btn1);
//    layout1->addWidget(edit1);
//    layout1->addWidget(edit2);
//    layout1->addWidget(edit3);

//    layout2->addWidget(label1);
//    layout3->addLayout(layout1);
//    layout3->addLayout(layout2);
//     btn1->setText("确定");
//     int a = edit1->text().toInt();
//     int b = edit3->text().toInt();

 layout4->addWidget(btn1,0,0);
 layout4->addWidget(edit1,0,1);
 layout4->addWidget(edit2,0,2);
 layout4->addWidget(edit3,0,3);

  layout4->addWidget(label1,1,2);
   btn1->setText("确定");

connect(btn1,SIGNAL(clicked()),this,SLOT(on_clicked()));
}

Widget::~Widget()
{

}
void Widget::on_clicked()
{
       int a = edit1->text().toInt();
        int b = edit3->text().toInt();
        if (edit2->text() == "+")
            label1->setText(QString::number(a + b));
        if (edit2->text() == "-")
            label1->setText(QString::number(a - b));
        if (edit2->text() == "*")
            label1->setText(QString::number(a * b));
        if (edit2->text() == "/")
        {
            if (b != 0)
                label1->setText(QString::number(a / b));
        }
}

 

效果图

Qt--实现简单加法计算器_第1张图片

 

Qt简单项目--加法计算器(详细代码注释) - Arroz - 博客园
https://www.cnblogs.com/ArrozZhu/p/8384427.html

QT实现简单的计算器 - kh971024的博客 - CSDN博客
https://blog.csdn.net/kh971024/article/details/78306517

你可能感兴趣的:(QT)