Qt设计:简单的小计算器
作者:互联网
##先看一下实现的效果
##细节
##mymainwindow.CPP
#include "mymainwindow.h"
#include "ui_mymainwindow.h"
#include "QString"
myMainWindow::myMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::myMainWindow)
{
ui->setupUi(this);
}
myMainWindow::~myMainWindow()
{
delete ui;
}
void myMainWindow::on_spinBox_valueChanged(int arg1)
{
bool ok;
int index;
QString n;
QString a=ui->spinBox->text();
QString b=ui->spinBox_2->text();
int c=a.toInt(&ok);
int d=b.toInt(&ok);
index=ui->comboBox->currentIndex();
switch(index)
{
case 0:
{
double f=c+d;
ui->lineEdit->setText(n.setNum(f));
break;
}
case 1:
{
double f=c-d;
ui->lineEdit->setText(n.setNum(f));
break;
}
case 2:
{
double f=c*d;
ui->lineEdit->setText(n.setNum(f));
break;
}
case 3:
{
double f=c/d;
ui->lineEdit->setText(n.setNum(f));
break;
}
}
}
void myMainWindow::on_spinBox_2_valueChanged(int arg1)
{
bool ok;
int index;
QString n;
QString a=ui->spinBox->text();
QString b=ui->spinBox_2->text();
int c=a.toInt(&ok);
int d=b.toInt(&ok);
index=ui->comboBox->currentIndex();
switch(index)
{
case 0:
{
double f=c+d;
ui->lineEdit->setText(n.setNum(f));
break;
}
case 1:
{
double f=c-d;
ui->lineEdit->setText(n.setNum(f));
break;
}
case 2:
{
double f=c*d;
ui->lineEdit->setText(n.setNum(f));
break;
}
case 3:
{
double f=c/d;
ui->lineEdit->setText(n.setNum(f));
break;
}
}
}
void myMainWindow::on_pushButton_clicked()
{
close();
}
##myMainWindow.h
#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class myMainWindow;
}
class myMainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit myMainWindow(QWidget *parent = 0);
~myMainWindow();
private slots:
void on_spinBox_valueChanged(int arg1);
void on_spinBox_2_valueChanged(int arg1);
void on_pushButton_clicked();
private:
Ui::myMainWindow *ui;
};
#endif // MYMAINWINDOW_H
##main.cpp
#include "mymainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
myMainWindow w;
w.show();
return a.exec();
}
标签:case,myMainWindow,int,计算器,spinBox,break,ui,简单,Qt 来源: https://blog.csdn.net/title71/article/details/79008422