Linux中使用qt实现简易计算器

  • 内容
  • 相关

    运行环境:Ubuntu Linux 、Qt4.8

    首先打开Qt Creator ---> 选择 "New Project" ---> Application ---> "Qt Widgets Application" ---> choose ---> 输入Name ---> Next ---> Next ---> 基类选择 QDialog ---> Next  ---> Finsh

jsq1.png

jsq2.png

    然后打开ui文件,拖入一个LineEdit 控件作为输出框,再拖几个Push Button 控件(1,2,3...+,-,*,/),如果觉得它不整齐的话可以进行布局。

    具体实现代码如下:

头文件dialog.h:

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

    QString str_x;
    QString str_y;
    int flag;
    char op;

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_4_clicked();

    void on_pushButton_6_clicked();

    void on_pushButton_7_clicked();

    void on_pushButton_8_clicked();

    void on_pushButton_10_clicked();

    void on_pushButton_11_clicked();

    void on_pushButton_12_clicked();

    void on_pushButton_15_clicked();

    void on_pushButton_5_clicked();

    void on_pushButton_9_clicked();

    void on_pushButton_13_clicked();

    void on_pushButton_17_clicked();

    void on_pushButton_16_clicked();

private:
    Ui::Dialog *ui;

    int z;
};

#endif // DIALOG_H

dialog.cpp文件:

#include "dialog.h"
#include "ui_dialog.h"

#include <QDebug>
#include <QMessageBox>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    flag = 0;
}

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

void Dialog::on_pushButton_clicked()
{
    ui->lineEdit->setText("");
}

void Dialog::on_pushButton_2_clicked()
{
    QString str;
    str += ui->lineEdit->text()+"1";

    if(!flag)
        str_x = str;
    else
        str_y = str;
    ui->lineEdit->setText(str);
    qDebug()<<"1 button str = "<< str << endl;
}

void Dialog::on_pushButton_3_clicked()
{
    QString str;
    str += ui->lineEdit->text()+"2";

    if(!flag)
        str_x = str;
    else
        str_y = str;
    ui->lineEdit->setText(str);
    qDebug()<<"2 button str = "<< str << endl;
}

void Dialog::on_pushButton_4_clicked()
{
    QString str;
    str += ui->lineEdit->text()+"3";

    if(!flag)
        str_x = str;
    else
        str_y = str;
    ui->lineEdit->setText(str);
    qDebug()<<"3 button str = "<< str << endl;
}

void Dialog::on_pushButton_6_clicked()
{
    QString str;
    str += ui->lineEdit->text()+"4";

    if(!flag)
        str_x = str;
    else
        str_y = str;
    ui->lineEdit->setText(str);
    qDebug()<<"4 button str = "<< str << endl;
}

void Dialog::on_pushButton_7_clicked()
{
    QString str;
    str += ui->lineEdit->text()+"5";

    if(!flag)
        str_x = str;
    else
        str_y = str;
    ui->lineEdit->setText(str);
    qDebug()<<"5 button str = "<< str << endl;
}

void Dialog::on_pushButton_8_clicked()
{
    QString str;
    str += ui->lineEdit->text()+"6";

    if(!flag)
        str_x = str;
    else
        str_y = str;
    ui->lineEdit->setText(str);
    qDebug()<<"6 button str = "<< str << endl;
}

void Dialog::on_pushButton_10_clicked()
{
    QString str;
    str += ui->lineEdit->text()+"7";

    if(!flag)
        str_x = str;
    else
        str_y = str;
    ui->lineEdit->setText(str);
    qDebug()<<"7 button str = "<< str << endl;
}

void Dialog::on_pushButton_11_clicked()
{
    QString str;
    str += ui->lineEdit->text()+"8";

    if(!flag)
        str_x = str;
    else
        str_y = str;
    ui->lineEdit->setText(str);
    qDebug()<<"8 button str = "<< str << endl;
}

void Dialog::on_pushButton_12_clicked()
{
    QString str;
    str += ui->lineEdit->text()+"9";

    if(!flag)
        str_x = str;
    else
        str_y = str;
    ui->lineEdit->setText(str);
    qDebug()<<"9 button str = "<< str << endl;
}

void Dialog::on_pushButton_15_clicked()
{
    QString str;
    str += ui->lineEdit->text()+"0";

    if(!flag)
        str_x = str;
    else
        str_y = str;
    ui->lineEdit->setText(str);
    qDebug()<<"0 button str = "<< str << endl;
}

void Dialog::on_pushButton_5_clicked()
{
    flag = 1;
    op = '+';
    ui->lineEdit->setText("");
}

void Dialog::on_pushButton_9_clicked()
{
    flag = 1;
    op = '-';
    ui->lineEdit->setText("");
}

void Dialog::on_pushButton_13_clicked()
{
    flag = 1;
    op = '*';
    ui->lineEdit->setText("");
}

void Dialog::on_pushButton_17_clicked()
{
    flag = 1;
    op = '/';
    ui->lineEdit->setText("");
}

void Dialog::on_pushButton_16_clicked()
{
    switch(op)
    {
    case '+':
        z = str_x.toInt() + str_y.toInt();
        break;
    case '-':
        z = str_x.toInt() - str_y.toInt();
        break;
    case '*':
        z = str_x.toInt() * str_y.toInt();
        break;
    case '/':
        if(str_y!=0){
            z =str_x.toInt() / str_y.toInt();
            break;
        }
        else
            QMessageBox::about(this,"Warning","Error,please");
        // default:
    }
    ui->lineEdit->setText(QString::number(z));
}

    运行效果图:

jsq.png

本文标签:

版权声明:若无特殊注明,本文皆为《尤尤》原创,转载请保留文章出处。

本文链接:Linux中使用qt实现简易计算器 - https://www.yxfseo.cn/post-141.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注