1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include "op.h"
4 #include <QDebug>
5
6 MainWindow::MainWindow(QWidget *parent) :
7 QMainWindow(parent),
8 ui(new Ui::MainWindow)
9 {
10 ui->setupUi(this);
11 }
12
13 MainWindow::~MainWindow()
14 {
15 delete ui;
16 }
17
18 const QString op1("+");
19 const QString op2("-");
20 const QString op3("*");
21 const QString op4("/");
22
23 void MainWindow::on_pushButton_clicked()
24 {
25 qDebug()<<"hello"<<endl;
26 op ops;
27 bool isok;
28 QString str1 = ui->num1->text();
29 QString str2 = ui->op->text();
30 QString str3 = ui->num2->text();
31 //保存结果
32 QString str4;
33 int a = str1.toInt(&isok,10);
34 int b = str3.toInt(&isok,10);
35
36 ops.seta(a);
37 ops.setb(b);
38
39 if(str2 == op1)
40 {
41 str4.sprintf("%d",ops.add());
42 qDebug()<<str4<<endl;
43 }
44 else if(str2 == op2)
45 {
46 str4.sprintf("%d",ops.sub());
47 qDebug()<<str4<<endl;
48 }
49 else if(str2 == op3)
50 {
51 str4.sprintf("%d",ops.mul());
52 qDebug()<<str4<<endl;
53 }
54 else if(str2 == op4)
55 {
56 str4.sprintf("%d",ops.divv());
57 qDebug()<<str4<<endl;
58 }
59 ui->textEdit->setText(str4);
60 }
61
62 void MainWindow::run()
63 {
64 qDebug()<<"hello"<<endl;
65 op ops;
66 bool isok;
67 QString str1 = ui->num1->text();
68 QString str2 = ui->op->text();
69 QString str3 = ui->num2->text();
70 //保存结果
71 QString str4;
72 int a = str1.toInt(&isok,10);
73 int b = str3.toInt(&isok,10);
74
75 ops.seta(a);
76 ops.setb(b);
77
78 if(str2 == op1)
79 {
80 str4.sprintf("%d",ops.add());
81 qDebug()<<str4<<endl;
82 }
83 else if(str2 == op2)
84 {
85 str4.sprintf("%d",ops.sub());
86 qDebug()<<str4<<endl;
87 }
88 else if(str2 == op3)
89 {
90 str4.sprintf("%d",ops.mul());
91 qDebug()<<str4<<endl;
92 }
93 else if(str2 == op4)
94 {
95 str4.sprintf("%d",ops.divv());
96 qDebug()<<str4<<endl;
97 }
98 ui->textEdit->setText(str4);
99 }
100
101
102 void MainWindow::on_num1_textChanged(const QString &arg1)
103 {
104 qDebug()<<"hello"<<endl;
105 op ops;
106 //父类存储子类对象的地址
107 QObject *pobj = &ops;
108
109 bool isok;
110 QString str1 = ui->num1->text();
111 int a = str1.toInt(&isok,10);
112 if(!isok)
113 {
114 ui->num1->clear();
115 }
116 }