QT 制作简易几何计算器
一、新建项目并添加类文件
二、ui界面设计
三、连接Combo Box对象与Stacked Widget对象
四、结果及源代码
一、新建项目并添加类文件
1.新建一个名为“calculate_area”的继承自mainWindow类的项目。
此时工程目录下有5个文件:mainwindow.h、mainwindow.cpp、mainwindow.ui、main.cpp、calculate_area.pro。
2.添加3个Qt设计类界面(Dialog without Buttons模板),类名分别为:area(计算面积)、length(计算周长)、volume(计算体积)。
此时工程目录下增加了6个文件:area.h、area.cpp、length.h、length.cpp、volume.h、volum.cpp
项目树形图如下所示:
二、ui界面设计
1.双击mainwin.ui,拖入三个push Button控件。如下图所示:
2.双击area.ui,拖入push Button、Label、lineedit、Stacked Widget、Combo Box控件
2.1 双击Combo Box,在编辑组合框内添加4个选项:长方形、正方形、圆形、三角形
2.2 Stacked Widget里添加4页(下图是第一页,第2~4页分别是正方形、圆形、三角形相关控件),设置成如下形式(并分别修改lineedit、push Button对象名,便于辨认)。
3 同理length.ui以及volum.ui界面设计如下:
三、连接Combo Box对象与Stacked Widget对象
以area类为例(class length 及class volume类似)。
在mainwindow.h下添加头文件:“area.h”、“length.h”、“volume.h”
1.双击mainwindow.ui。选中“面积计算”button,右键点击“转到槽”选择”clicked()”。在槽函数里添加 如下代码:
void MainWindow::on_area_btn_clicked()
{
area area_model;
area_model.setWindowTitle("面积");
area_model.exec();
}
2.双击area.ui。选中combo Box对象,右键点击“转到槽”选择“currentIndexChanged(QString)”。在槽函数里添加如下代码:
void area::on_comboBox_currentTextChanged(const QString &arg1)
{
//若comboBox当前显示的是“长方形选项”,则设置当前stackedWidget页码索引为0(第0页为首页)
if(ui->comboBox->currentText()=="长方形")
{
ui->stackedWidget->setCurrentIndex(0);
}
if(ui->comboBox->currentText()=="正方形")
{
ui->stackedWidget->setCurrentIndex(1);
}
if(ui->comboBox->currentText()=="圆形")
{
ui->stackedWidget->setCurrentIndex(2);
}
if(ui->comboBox->currentText()=="三角形")
{
ui->stackedWidget->setCurrentIndex(3);
}
}
3.双击area.ui。选中“计算”按钮,右键点击“转到槽”选择“clicked()”。在槽函数里添加如下代码:
void area::on_cal_clicked()
{
//获取当前页码索引,获取当前用户输入数据,分别计算各图形的面积
if(ui->stackedWidget->currentIndex()==0)
{
double heigh = ui->Rec_line_heigh->text().toDouble();
double width = ui->Rec_line_width->text().toDouble();
ui->Rec_line_area->setText(QString::number(heigh * width));
}
if(ui->stackedWidget->currentIndex()==1)
{
double edge = ui->Squ_line_width->text().toDouble();
ui->Squ_line_area ->setText(QString::number(edge * edge));
}
if(ui->stackedWidget->currentIndex()==2)
{
double radius = ui->Cir_line_radius ->text().toDouble();
ui->Cir_line_area->setText(QString::number(3.1415*radius*radius));
}
if(ui->stackedWidget->currentIndex()==3)
{
double heigh = ui->Tri_line_heigh ->text().toDouble();
double width = ui->Tri_line_eage ->text().toDouble();
ui->Tri_line_area->setText(QString::number(0.5*heigh*width));
}
}
四、结果及源代码
生成exe可执行文件
主界面:
点击任意一个选项进行相关计算(如面积计算):
选择计算长方形面积:输入长宽数据:
点击计算按钮,得出结果:
以下是源代码:
/********************mainwindow.h****************************/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDialog>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_area_btn_clicked();
void on_length_btn_clicked();
void on_pushButton_3_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
/********************mainwindow.cpp****************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QComboBox>
#include "area.h"
#include "length.h"
#include "volume.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_area_btn_clicked()
{
area area_model;
area_model.setWindowTitle("面积");
area_model.exec();
}
void MainWindow::on_length_btn_clicked()
{
length length_model;
length_model.setWindowTitle("周长");
length_model.exec();
}
void MainWindow::on_pushButton_3_clicked()
{
volume volume_model;
volume_model.setWindowTitle("体积");
volume_model.exec();
}
/**********************area.h*********************************/
#ifndef AREA_H
#define AREA_H
#include <QDialog>
namespace Ui {
class area;
}
class area : public QDialog
{
Q_OBJECT
public:
explicit area(QWidget *parent = 0);
~area();
private slots:
void on_comboBox_currentTextChanged(const QString &arg1);
void on_cal_clicked();
private:
Ui::area *ui;
};
#endif // AREA_H
/**********************area.cpp*********************************/
#include "area.h"
#include "ui_area.h"
#include <QDebug>
area::area(QWidget *parent) :
QDialog(parent),
ui(new Ui::area)
{
ui->setupUi(this);
}
area::~area()
{
delete ui;
}
void area::on_comboBox_currentTextChanged(const QString &arg1)
{
if(ui->comboBox->currentText()=="长方形")
{
ui->stackedWidget->setCurrentIndex(0);
}
if(ui->comboBox->currentText()=="正方形")
{
ui->stackedWidget->setCurrentIndex(1);
}
if(ui->comboBox->currentText()=="圆形")
{
ui->stackedWidget->setCurrentIndex(2);
}
if(ui->comboBox->currentText()=="三角形")
{
ui->stackedWidget->setCurrentIndex(3);
}
}
void area::on_cal_clicked()
{
if(ui->stackedWidget->currentIndex()==0)
{
double heigh = ui->Rec_line_heigh->text().toDouble();
double width = ui->Rec_line_width->text().toDouble();
ui->Rec_line_area->setText(QString::number(heigh * width));
}
if(ui->stackedWidget->currentIndex()==1)
{
double edge = ui->Squ_line_width->text().toDouble();
ui->Squ_line_area ->setText(QString::number(edge * edge));
}
if(ui->stackedWidget->currentIndex()==2)
{
double radius = ui->Cir_line_radius ->text().toDouble();
ui->Cir_line_area->setText(QString::number(3.1415*radius*radius));
}
if(ui->stackedWidget->currentIndex()==3)
{
double heigh = ui->Tri_line_heigh ->text().toDouble();
double width = ui->Tri_line_eage ->text().toDouble();
ui->Tri_line_area->setText(QString::number(0.5*heigh*width));
}
}
/**********************length.h********************************/
#ifndef LENGTH_H
#define LENGTH_H
#include <QDialog>
namespace Ui {
class length;
}
class length : public QDialog
{
Q_OBJECT
public:
explicit length(QWidget *parent = 0);
~length();
private slots:
void on_comboBox_currentIndexChanged(const QString &arg1);
void on_cal_clicked();
private:
Ui::length *ui;
};
#endif // LENGTH_H
/*********************length.cpp*******************************/
#include "length.h"
#include "ui_length.h"
length::length(QWidget *parent) :
QDialog(parent),
ui(new Ui::length)
{
ui->setupUi(this);
}
length::~length()
{
delete ui;
}
void length::on_comboBox_currentIndexChanged(const QString &arg1)
{
if(ui->comboBox->currentText()=="长方形")
{
ui->stackedWidget->setCurrentIndex(0);
}
if(ui->comboBox->currentText()=="正方形")
{
ui->stackedWidget->setCurrentIndex(1);
}
if(ui->comboBox->currentText()=="圆形")
{
ui->stackedWidget->setCurrentIndex(2);
}
if(ui->comboBox->currentText()=="三角形")
{
ui->stackedWidget->setCurrentIndex(3);
}
}
void length::on_cal_clicked()
{
if(ui->stackedWidget->currentIndex()==0)
{
double heigh = ui->Rec_line_heigh->text().toDouble();
double width = ui->Rec_line_width->text().toDouble();
ui->Rec_line_length ->setText(QString::number(2*(heigh + width)));
}
if(ui->stackedWidget->currentIndex()==1)
{
double edge = ui->Squ_line_width->text().toDouble();
ui->Squ_line_length ->setText(QString::number(4*edge));
}
if(ui->stackedWidget->currentIndex()==2)
{
double radius = ui->Cir_line_radius ->text().toDouble();
ui->Cir_line_length->setText(QString::number(2*3.1415*radius));
}
if(ui->stackedWidget->currentIndex()==3)
{
double edge1 = ui->Tri_line_eage1 ->text().toDouble();
double edge2 = ui->Tri_line_eage2 ->text().toDouble();
double edge3 = ui->Tri_line_eage3 ->text().toDouble();
ui->Tri_line_length->setText(QString::number(edge1+edge2+edge3));
}
}
/**********************volume.h*******************************/
#ifndef VOLUME_H
#define VOLUME_H
#include <QDialog>
namespace Ui {
class volume;
}
class volume : public QDialog
{
Q_OBJECT
public:
explicit volume(QWidget *parent = 0);
~volume();
private slots:
void on_comboBox_currentIndexChanged(const QString &arg1);
void on_cal_clicked();
private:
Ui::volume *ui;
};
#endif // VOLUME_H
/**********************volume.cpp******************************/
#include "volume.h"
#include "ui_volume.h"
volume::volume(QWidget *parent) :
QDialog(parent),
ui(new Ui::volume)
{
ui->setupUi(this);
}
volume::~volume()
{
delete ui;
}
void volume::on_comboBox_currentIndexChanged(const QString &arg1)
{
if(ui->comboBox->currentText()=="长方体")
{
ui->stackedWidget->setCurrentIndex(0);
}
if(ui->comboBox->currentText()=="正方体")
{
ui->stackedWidget->setCurrentIndex(1);
}
if(ui->comboBox->currentText()=="圆柱体")
{
ui->stackedWidget->setCurrentIndex(2);
}
if(ui->comboBox->currentText()=="圆锥体")
{
ui->stackedWidget->setCurrentIndex(3);
}
if(ui->comboBox->currentText()=="球体")
{
ui->stackedWidget->setCurrentIndex(4);
}
}
void volume::on_cal_clicked()
{
if(ui->stackedWidget->currentIndex()==0)
{
double heigh = ui->Rec_line_heigh->text().toDouble();
double width = ui->Rec_line_width->text().toDouble();
double longs = ui->Rec_line_long->text().toDouble();
ui->Rec_line_volume->setText(QString::number(heigh * width *longs));
}
if(ui->stackedWidget->currentIndex()==1)
{
double edge = ui->Squ_line_width->text().toDouble();
ui->Squ_line_volume ->setText(QString::number(edge * edge * edge));
}
if(ui->stackedWidget->currentIndex()==2)
{
double radius = ui->Cir_line_radius ->text().toDouble();
double heigh = ui->Cir_line_heigh->text().toDouble();
ui->Cir_line_volume->setText(QString::number(3.1415*radius * radius * heigh));
}
if(ui->stackedWidget->currentIndex()==3)
{
double heigh = ui->Cir_tri_line_heigh ->text().toDouble();
double radius = ui->Cir_tri_line_radius ->text().toDouble();
ui->Cir_tri_line_volume->setText(QString::number(1.0/3.0*(3.1415 * radius * radius) * heigh));
}
if(ui->stackedWidget->currentIndex()==4)
{
double radius = ui->ball_line_eage ->text().toDouble();
ui->ball_line_volume->setText(QString::number(4.0/3.0 * 3.1415 * radius * radius * radius));
}
}
/*************************main.cpp****************************/
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowTitle("几何计算");
w.show();
return a.exec();
}
上一篇: 创建mysql表分区的方法
下一篇: Jquery知识点2