Qt 计算器

 widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent),
     ui(new Ui::Widget)
{
    ui->setupUi(this);
    QFont f("仿宋",13);//ziti//
   ui->mylinedit->setFont(f);
   QIcon con("D:\\Qt creator\\run\\001.jpg");
   ui->tui->setIcon(con);
   ui->deng->setStyleSheet("background:pink");
   ui->clear->setStyleSheet("background:pink");
   ui->tui->setStyleSheet("background:pink");
}

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


void Widget::on_no1_clicked()
{
    expression+="1";
    ui->mylinedit->setText(expression);
}

void Widget::on_no2_clicked()
{
    expression+="2";
    ui->mylinedit->setText(expression);
}

void Widget::on_no3_clicked()
{
    expression+="3";
    ui->mylinedit->setText(expression);
}

void Widget::on_no4_clicked()
{
    expression+="4";
    ui->mylinedit->setText(expression);
}

void Widget::on_no5_clicked()
{
    expression+="5";
    ui->mylinedit->setText(expression);
}

void Widget::on_no6_clicked()
{
    expression+="6";
    ui->mylinedit->setText(expression);
}

void Widget::on_no7_clicked()
{
    expression+="7";
    ui->mylinedit->setText(expression);
}

void Widget::on_no8_clicked()
{
    expression+="8";
    ui->mylinedit->setText(expression);
}

void Widget::on_no9_clicked()
{
    expression+="9";
    ui->mylinedit->setText(expression);
}

void Widget::on_add_clicked()
{
    expression+="+";
    ui->mylinedit->setText(expression);
}

void Widget::on_minus_clicked()
{
    expression+="-";
    ui->mylinedit->setText(expression);
}

void Widget::on_cheng_clicked()
{
    expression+="*";
    ui->mylinedit->setText(expression);
}

void Widget::on_chu_clicked()
{
    expression+="/";
    ui->mylinedit->setText(expression);
}

void Widget::on_you_clicked()
{
    expression+=")";
    ui->mylinedit->setText(expression);
}

void Widget::on_zuo_clicked()
{
    expression+="(";
    ui->mylinedit->setText(expression);
}

void Widget::on_no0_clicked()
{
    expression+="0";
    ui->mylinedit->setText(expression);
}


void Widget::on_clear_clicked()
{
    expression.clear();
    ui->mylinedit->clear();
}

void Widget::on_tui_clicked()
{
    expression.chop(1);
    ui->mylinedit->setText(expression);
}


void Widget::on_deng_clicked()
{
    QString value;
        value = ui->mylinedit->text();
        if(value.isEmpty())
            return;
                    if(value.indexOf("("&&")")!=-1){
                        int a;
                        a=value.indexOf("(");
                        int q;
                        q=value.indexOf(")");

                        int b;
                        b=value.leftRef(q).toInt();
                        int c;
                        c=value.rightRef(a).toInt();

                    }
                    if(value.indexOf("*")!=-1)
                    {
                        int a;
                        a=value.indexOf("*");
                        int b;
                        b=value.leftRef(a).toInt();
                        int c;
                        c=value.rightRef(value.size()-a-1).toInt();
                        int d;
                        d=(b*c);
                        ui->mylinedit->setText(QString::number(d));
                    }
                    if(value.indexOf("/")!=-1)
                    {
                        double a;
                        a=value.indexOf("/");
                        double b;
                        b=value.leftRef(a).toDouble();
                        double c;
                        c=value.rightRef(value.size()-a-1).toDouble();
                        double d;
                        d=(b/c);
                        ui->mylinedit->setText(QString::number(d));
                    }
                    if(value.indexOf("+")!=-1)
                    {
                        double a;
                        a=value.indexOf("+");
                        double b;
                        b=value.leftRef(a).toDouble();
                        double c;
                        c=value.rightRef(a).toDouble();
                        double d;
                        d=(b+c);
                        ui->mylinedit->setText(QString::number(d));
                    }
                    if(value.indexOf("-")!=-1)
                    {
                        int a;
                        a=value.indexOf("-");
                        int b;
                        b=value.leftRef(a).toInt();
                        int c;
                        c=value.rightRef(value.size()-a-1).toInt();
                        int d;
                        d=b-c;
                        ui->mylinedit->setText(QString::number(d));
                    }
            }

main.cpp

#include "widget.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

widge.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_no1_clicked();

    void on_no2_clicked();

    void on_no3_clicked();

    void on_no4_clicked();

    void on_no5_clicked();

    void on_no6_clicked();

    void on_no7_clicked();

    void on_no8_clicked();

    void on_no9_clicked();

    void on_add_clicked();

    void on_minus_clicked();

    void on_cheng_clicked();

    void on_chu_clicked();

    void on_you_clicked();

    void on_zuo_clicked();

    void on_no0_clicked();

    void on_deng_clicked();

    void on_clear_clicked();

    void on_tui_clicked();

private:
    Ui::Widget *ui;
    QString expression;
};
#endif // WIDGET_H

你可能感兴趣的:(qt,ui,开发语言)