我们始终是路人

I'll try anything once.学习笔记

计算器-QT界面编程

程序界面:
程序界面

//main.cpp
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])

{
    QApplication::addLibraryPath("./plugins");//设置DLL文件目录
    QApplication a(argc, argv);
    MainWindow w;
    w.show();//显示
    return a.exec();
}
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    num1=num2="0";//初始化操作数1,2的值
    fuhao=' ';//初始化符号值为空
    this->setFixedSize(414,280);//固定窗口大小
    ui->lineEdit->setAlignment(Qt::AlignRight);//面板设置右对齐
    ui->lineEdit->setFixedSize(260,25);//面板初始化大小
    ui->lineEdit->setText("0.000");//面板初始化
    ui->lineEdit->setFont(QFont("Timers" , 15 ,  QFont::Bold));//面板字体及大小
    ui->lineEdit_2->setAlignment(Qt::AlignRight);//面板设置右对齐
    ui->lineEdit_2->setFixedSize(260,25);//面板初始化大小
    ui->lineEdit_2->setText("0.000");//面板初始化
    ui->lineEdit_2->setFont(QFont("Timers" , 15 ,  QFont::Bold));//面板字体及大小
    //QMessageBox::warning(NULL, "注意", "本计算器仅能进行二则计算!");//消息框
}

MainWindow::~MainWindow()//析构函数
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()//数字1~9
{
    s+="1";//更新字符串s
    if(num2=="0")//
    {
        num2="1";
    }
    else
    {
        num2.append('1');//连续点击同一按钮,追加字符 1
    }
    ui->lineEdit->setText(s);//显示字符串s的内容
}

void MainWindow::on_pushButton_2_clicked()
{
    s+="2";
    if(num2=="0")
    {
        num2="2";
    }
    else
    {
        num2.append('2');
    }
    ui->lineEdit->setText(s);
}

void MainWindow::on_pushButton_3_clicked()
{
    s+="3";
    if(num2=="0")
    {
        num2="3";
    }
    else
    {
        num2.append('3');
    }
    ui->lineEdit->setText(s);
}


void MainWindow::on_pushButton_4_clicked()
{
    s+="4";
    if(num2=="0")
    {
        num2="4";
    }
    else
    {
        num2.append('4');
    }
    ui->lineEdit->setText(s);
}

void MainWindow::on_pushButton_5_clicked()
{
    s+="5";
    if(num2=="0")
    {
        num2="5";
    }
    else
    {
        num2.append('5');
    }
    ui->lineEdit->setText(s);
}

void MainWindow::on_pushButton_6_clicked()
{
    s+="6";
    if(num2=="0")
    {
        num2="6";
    }
    else
    {
        num2.append('6');
    }
    ui->lineEdit->setText(s);
}

void MainWindow::on_pushButton_7_clicked()
{
    s+="7";
    if(num2=="0")
    {
        num2="7";
    }
    else
    {
        num2.append('7');
    }
    ui->lineEdit->setText(s);
}

void MainWindow::on_pushButton_8_clicked()
{
    s+="8";
    if(num2=="0")
    {
        num2="8";
    }
    else
    {
        num2.append('8');
    }
    ui->lineEdit->setText(s);
}

void MainWindow::on_pushButton_9_clicked()
{
    s+="9";
    if(num2=="0")
    {
        num2="9";
    }
    else
    {
        num2.append('9');
    }
    ui->lineEdit->setText(s);
}

void MainWindow::on_pushButton_15_clicked()//数字0
{
    s+="0";
    if(num2=="0")
    {
        num2="0";
    }
    else
    {
        num2.append('0');
    }
    ui->lineEdit->setText(s);
}
void MainWindow::on_pushButton_10_clicked()//+
{
    double a,b;
    s+="+";
    ui->lineEdit->setText(s);
    if(fuhao==' ')
    {
        num1=num2;
        num2="0";
        fuhao='+';
    }
    else
    {
        a=num2.toDouble();//将字符串转换浮点类型
        b=num1.toDouble();
        switch(fuhao)
        {
            case '+':a+=b;break;
            case '-':a=b-a;break;
            case '*':a*=b;break;
            case '/':a=b/a;break;
        }
        num1=QString::number(a,'f',3);//转换回来
        num2="0";
        fuhao='+';
        ui->lineEdit_2->setText(num1);
    }

}

void MainWindow::on_pushButton_11_clicked()//-
{
    double a,b;
    s+="-";
    ui->lineEdit->setText(s);
    if(fuhao==' ')
    {
        num1=num2;
        num2="0";
        fuhao='-';
    }
    else
    {
        a=num2.toDouble();
        b=num1.toDouble();
        switch(fuhao)
        {
            case '+':a+=b;break;
            case '-':a=b-a;break;
            case '*':a*=b;break;
            case '/':a=b/a;break;
        }
        num1=QString::number(a,'f',3);
        num2="0";
        fuhao='-';
        ui->lineEdit_2->setText(num1);
    }
}

void MainWindow::on_pushButton_18_clicked()//负数
{
    s+="-";
    if(num2=="0")
    {
        num2="-";
    }
    else
    {
        num2.append('-');
    }
    ui->lineEdit->setText(s);
}

void MainWindow::on_pushButton_12_clicked()//*
{
    double a,b;
    s+="*";
    ui->lineEdit->setText(s);
    if(fuhao==' ')
    {
        num1=num2;
        num2="0";
        fuhao='*';
    }
    else
    {
        a=num2.toDouble();
        b=num1.toDouble();
        switch(fuhao)
        {
            case '+':a+=b;break;
            case '-':a=b-a;break;
            case '*':a*=b;break;
            case '/':a=b/a;break;
        }
        num1=QString::number(a,'f',3);
        num2="0";
        fuhao='*';
        ui->lineEdit_2->setText(num1);
    }
}

void MainWindow::on_pushButton_13_clicked()// /
{
    double a,b;
    s+="/";
    ui->lineEdit->setText(s);
    if(fuhao==' ')
    {
        num1=num2;
        num2="0";
        fuhao='/';
    }
    else
    {
        a=num2.toDouble();
        b=num1.toDouble();
        switch(fuhao)
        {
            case '+':a+=b;break;
            case '-':a=b-a;break;
            case '*':a*=b;break;
            case '/':a=b/a;break;
        }
        num1=QString::number(a,'f',3);
        num2="0";
        fuhao='/';
        ui->lineEdit_2->setText(num1);
    }
}

void MainWindow::on_pushButton_14_clicked()// =
{
    double a,b;
    if(fuhao==' ')
    {
        num1=num2;
        num2="0";
        fuhao='=';
    }
    else
    {
        a=num2.toDouble();
        b=num1.toDouble();
        switch(fuhao)
        {
            case '+':a+=b;break;
            case '-':a=b-a;break;
            case '*':a*=b;break;
            case '/':a=b/a;break;
        }
        num1=QString::number(a,'f',3);
        num2="0";
        ui->lineEdit_2->setText(num1);
    }
}

void MainWindow::on_pushButton_16_clicked()//归零
{
    s="";
    num1=num2="0";
    ui->lineEdit->setText("0.000");
    ui->lineEdit_2->setText("0.000");
    fuhao=' ';
}

void MainWindow::on_pushButton_17_clicked()//小数点
{
    s+=".";
    if(num2=="0")
    {
        num2=".";
    }
    else
    {
        num2.append('.');
    }
    ui->lineEdit->setText(s);
}
void MainWindow::on_actionAa_triggered()
{
    QMessageBox::about(NULL, "关于", "计算器 \n By: \n Build:2016.10.25");//消息盒子
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
    int uKey = event->key();
    switch(uKey)
    {//键盘事件处理
        case Qt::Key_1:emit ui->pushButton->click();break;
        case Qt::Key_2:emit ui->pushButton_2->click();break;
        case Qt::Key_3:emit ui->pushButton_3->click();break;
        case Qt::Key_4:emit ui->pushButton_4->click();break;
        case Qt::Key_5:emit ui->pushButton_5->click();break;
        case Qt::Key_6:emit ui->pushButton_6->click();break;
        case Qt::Key_7:emit ui->pushButton_7->click();break;
        case Qt::Key_8:emit ui->pushButton_8->click();break;
        case Qt::Key_9:emit ui->pushButton_9->click();break;
        case Qt::Key_0:emit ui->pushButton_15->click();break;
        case Qt::Key_Plus:emit ui->pushButton_10->click();break;//+
        case Qt::Key_Minus:emit ui->pushButton_11->click();break;//-
        case Qt::Key_Asterisk:emit ui->pushButton_12->click();break;//*
        case Qt::Key_Slash:emit ui->pushButton_13->click();break;// /
        case Qt::Key_Enter:emit ui->pushButton_14->click();break;//=
        case Qt::Key_Equal:emit ui->pushButton_14->click();break;//=
        case Qt::Key_Delete:emit ui->pushButton_16->click();break;//归零
        case Qt::Key_Period:emit ui->pushButton_17->click();break;//小数点
    }
}

//mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>411</width>
    <height>282</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>计算器</string>
  </property>
  <property name="windowIcon">
   <iconset>
    <normaloff>../../Desktop/icons.png</normaloff>../../Desktop/icons.png</iconset>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QWidget" name="">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>3</y>
      <width>391</width>
      <height>251</height>
     </rect>
    </property>
    <layout class="QVBoxLayout" name="verticalLayout_3">
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout_6">
       <item>
        <layout class="QVBoxLayout" name="verticalLayout">
         <item>
          <widget class="QLineEdit" name="lineEdit">
           <property name="readOnly">
            <bool>true</bool>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QLineEdit" name="lineEdit_2">
           <property name="readOnly">
            <bool>true</bool>
           </property>
          </widget>
         </item>
        </layout>
       </item>
       <item>
        <layout class="QVBoxLayout" name="verticalLayout_2">
         <item>
          <widget class="QPushButton" name="pushButton_16">
           <property name="text">
            <string>归零</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QPushButton" name="pushButton_17">
           <property name="text">
            <string>小数点</string>
           </property>
          </widget>
         </item>
        </layout>
       </item>
      </layout>
     </item>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout">
       <item>
        <widget class="QPushButton" name="pushButton">
         <property name="text">
          <string>1</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="pushButton_2">
         <property name="text">
          <string>2</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="pushButton_10">
         <property name="text">
          <string>+</string>
         </property>
        </widget>
       </item>
      </layout>
     </item>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout_2">
       <item>
        <widget class="QPushButton" name="pushButton_3">
         <property name="text">
          <string>3</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="pushButton_4">
         <property name="text">
          <string>4</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="pushButton_11">
         <property name="text">
          <string>-</string>
         </property>
        </widget>
       </item>
      </layout>
     </item>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout_3">
       <item>
        <widget class="QPushButton" name="pushButton_5">
         <property name="text">
          <string>5</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="pushButton_6">
         <property name="text">
          <string>6</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="pushButton_12">
         <property name="text">
          <string>*</string>
         </property>
        </widget>
       </item>
      </layout>
     </item>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout_4">
       <item>
        <widget class="QPushButton" name="pushButton_7">
         <property name="text">
          <string>7</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="pushButton_8">
         <property name="text">
          <string>8</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="pushButton_13">
         <property name="text">
          <string>/</string>
         </property>
        </widget>
       </item>
      </layout>
     </item>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout_5">
       <item>
        <widget class="QPushButton" name="pushButton_9">
         <property name="text">
          <string>9</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="pushButton_15">
         <property name="text">
          <string>0</string>
         </property>
         <property name="autoDefault">
          <bool>false</bool>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="pushButton_18">
         <property name="text">
          <string>负号</string>
         </property>
        </widget>
       </item>
      </layout>
     </item>
     <item>
      <widget class="QPushButton" name="pushButton_14">
       <property name="text">
        <string>=</string>
       </property>
      </widget>
     </item>
    </layout>
   </widget>
   <zorder>layoutWidget</zorder>
   <zorder>pushButton_16</zorder>
   <zorder>pushButton_16</zorder>
   <zorder>pushButton_16</zorder>
   <zorder>pushButton_17</zorder>
   <zorder>lineEdit_2</zorder>
   <zorder></zorder>
   <zorder>pushButton_18</zorder>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>411</width>
     <height>23</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuUs">
    <property name="title">
     <string>帮助</string>
    </property>
    <addaction name="actionAa"/>
   </widget>
   <widget class="QMenu" name="menu">
    <property name="title">
     <string>注意:运算顺序从左向右</string>
    </property>
   </widget>
   <addaction name="menuUs"/>
   <addaction name="menu"/>
  </widget>
  <action name="actionAa">
   <property name="text">
    <string>关于</string>
   </property>
   <property name="toolTip">
    <string>关于</string>
   </property>
  </action>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a1837634447/article/details/52963780
文章标签: 界面 编程
个人分类: QT C++
想对作者说点什么? 我来说一句

QT计算器

2017年09月08日 7KB 下载

QT做的简易计算器源码

2009年12月17日 12KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭