QT 简易计算机

在c站没发现一个完整可运行的项目 我来补充一篇好吧

本次简易计算机 ui界面由代码实现
整体轮廓如下

QT 简易计算机_第1张图片

Mainwindow.h文件

用来声明槽函数 和 用到的变量

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:
    void digitClicked();   //声明函数
    void operatorClicked();
    void equalClicked();
    void clearClicked();
    void mgw();
private:
    QLineEdit *le;      ///声明变量
    QPushButton *buttons[16];
    QString line="0";
    QGridLayout *gridLayout;
    QString a[10];
    QString b[6];
    QChar ch;
    qint8 flag=0;
    qint32 leftint=0,rightint=0;
};

#endif // MAINWINDOW_H


Main文件:

#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

Mainwindow.cpp

用来写 槽函数的具体方法
以及 控件的建立和摆放

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //输入框
    le = new QLineEdit();
    le->setParent(this);
    le->setReadOnly(true);
    le->setAlignment(Qt::AlignRight);
    le->setText("0");
//    string a[10]={"7","8","9","4","5","6","1","2","3","0"};
//    string b[6]={"+","-","*","C","=","/"};
    for(int i=0,j=7;i<3;j++,i++) a[i]=QString::number(j,10);   //存储 按键 信息
    for(int i=3,j=4;i<6;j++,i++) a[i]=QString::number(j,10);
    for(int i=6,j=1;i<9;j++,i++) a[i]=QString::number(j,10);
    a[9]=QString::number(0,10);
    b[0]="+"; b[1]="-"; b[2]="*"; b[3]="C"; b[4]="="; b[5]="/"; //存储运算按钮信息

    int cnt=0;
    int ans=0;
    int acnt=0;
    //按钮
    gridLayout = new QGridLayout();
    gridLayout->addWidget(le, 0, 0, 1, 4);
    for(int i = 0; i < 4; i++)    //对每个按钮进行创建  并且建立信号与槽关系
        {
            for(int j = 0; j <4 ; j++)
            {
                if((j==3)||(i==3&&(j==1||j==2))){
                    buttons[cnt] = new QPushButton(b[ans]);
                    if(j==3)
                        connect(buttons[cnt], SIGNAL(clicked()), this, SLOT(digitClicked()));
                    else if(b[ans]=="C"){
                        connect(buttons[cnt], SIGNAL(clicked()), this, SLOT(clearClicked()));
                    }else{
                        connect(buttons[cnt], SIGNAL(clicked()), this, SLOT(mgw()));
                    }
                    gridLayout->addWidget(buttons[cnt], i+1, j);
                    ans++;
                    cnt++;
                    continue;
                }

                buttons[cnt] = new QPushButton(a[acnt]);
                gridLayout->addWidget(buttons[cnt], i+1, j);
                connect(buttons[cnt], SIGNAL(clicked()), this, SLOT(digitClicked()));
                acnt++;
                cnt++;
            }
        }
       QWidget *centralWidget = new QWidget(this);
       centralWidget->setLayout(gridLayout);
       setCentralWidget(centralWidget);



}
MainWindow::~MainWindow()
{
}

void MainWindow::mgw(){    // 按下等号后统一运算
    QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
    line=le->text();
    int len=line.size();
    int flag=0;
    leftint=0;
    rightint=0;
    for(int i=0;i<len;i++){
        if(line[i]=='+'||line[i]=='-'||line[i]=='*'||line[i]=='/'){
            flag=1;
            ch=line[i];
            continue;
        }
        char cd;
        cd=line[i].toLatin1();
        if(flag) rightint=rightint*10+(cd-'0');
        else leftint=leftint*10+(cd-'0');
    }
    if(flag==0) return;        //如果不存在运算符 保留当前数字 (特判)
    flag=0;
        if(ch=='+')line = QString::number(leftint + rightint);
        if(ch=='-')line = QString::number(leftint - rightint);
        if(ch=='*')line = QString::number(leftint * rightint);
        if(ch=='/')line = QString::number(leftint / rightint);

    le->setText(line);
}


void MainWindow::digitClicked()   //插入数值
{ 
    QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
    if (le->text() == "0") {
        le->setText(clickedButton->text());
    } else {
        le->setText(le->text() + clickedButton->text());
    }
}


void MainWindow::clearClicked()   //清屏函数
{
    le->setText("0");
    line = "0";
    flag=0;
}

QT 简易计算机_第2张图片
QT 简易计算机_第3张图片
QT 简易计算机_第4张图片

在此补充几句话 , 放代码就好好放 你只放一点对新手很不友好啊 我真服了!!!

最后 水平有限 不足请指正

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