3.QT计算机实战

  • mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        void run();
        ~MainWindow();
    
    private slots:
        void on_pushButton_clicked();
    
        void on_num1_textChanged(const QString &arg1);
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H

     

  • mainwindow.cpp
      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 }

     

  • op.h
     1 #ifndef OP_H
     2 #define OP_H
     3 
     4 #include <QObject>
     5 
     6 class op : public QObject
     7 {
     8     Q_OBJECT
     9 public:
    10     explicit op(QObject *parent = nullptr);
    11     int add();
    12     int sub();
    13     int mul();
    14     int divv();
    15 
    16 public:
    17     void seta(int data);
    18     void setb(int data);
    19     int geta();
    20     int getb();
    21 
    22 private:
    23     int a;
    24     int b;
    25 
    26 
    27 signals:
    28 
    29 public slots:
    30 };
    31 
    32 #endif // OP_H

     

  • op.cpp
     1 #include "op.h"
     2 
     3 op::op(QObject *parent) : QObject(parent)
     4 {
     5     a = b = 0;
     6 }
     7 
     8 int op::add()
     9 {
    10     return a+b;
    11 }
    12 
    13 int op::sub()
    14 {
    15     return a-b;
    16 }
    17 
    18 int op::mul()
    19 {
    20     return a*b;
    21 }
    22 
    23 int op::divv()
    24 {
    25     return a/b;
    26 }
    27 
    28 void op::seta(int data)
    29 {
    30     a = data;
    31 }
    32 
    33 void op::setb(int data)
    34 {
    35     b = data;
    36 }
    37 
    38 int op::geta()
    39 {
    40     return a;
    41 }
    42 
    43 int op::getb()
    44 {
    45     return b;
    46 }

     

  • main.cpp
     1 #include "mainwindow.h"
     2 #include <QApplication>
     3 
     4 int main(int argc, char *argv[])
     5 {
     6     QApplication a(argc, argv);
     7     MainWindow w;
     8     w.show();
     9 
    10     return a.exec();
    11 }

     

posted @ 2018-04-07 17:15  喵小喵~  阅读(222)  评论(0编辑  收藏  举报