最简单的Qt程序:根据用户所输入圆半径计算圆面积

版权声明:分享、借鉴或使用刁肥宅的代码请务必注明出处!刁肥宅保留追究代码剽窃者的一切权利! https://blog.csdn.net/u25th_engineer/article/details/81878005

       这两天刚开始接触Qt5,所用环境为Qt creator 4.7.0社区版。这个编译器有许多令人难以理喻的bug,譬如:不能在程序中出现中文字符,否则代码编译会报错,而且一旦用户在编辑界面键入中文字符后,光标就消失了,刚开始我还以为又是哪的配置错误导致的。呜啦啦!个人觉得Qt creator 4.7.0的代码补全功能也没有VS 2017好用。且行且珍惜吧,使用开源软件从我做起!

       下面这是编译的书上例题代码之一,Qt其实也没那么难嘛~

      请留意下面三张图里程序的图标!

       工程文件circArea2.pro:

#-------------------------------------------------
#
# Project created by QtCreator 2018-08-20T20:25:12
#
#-------------------------------------------------

QT       += core gui widgets

TARGET = circleArea2
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        circlearea.cpp

HEADERS += \
        circlearea.h

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

       头文件circlearea.h:

//circlearea.h

#ifndef CIRCLEAREA_H
#define CIRCLEAREA_H

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>


class circleArea : public QDialog
{
    Q_OBJECT

public:
    circleArea(QWidget *parent = 0);
    ~circleArea();
private:
    QLabel *label1,*label2;
    QLineEdit *lineEdit;
    QPushButton *button;
private slots:
    void showArea();
};

#endif // CIRCLEAREA_H

       源文件circlearea.cpp:

//circlearea.cpp

#include "circlearea.h"
#include <QGridLayout>

const static double PI = 3.1415926535;

circleArea::circleArea(QWidget *parent)
    : QDialog(parent)
{
    label1 = new QLabel(this);
    label1->setText(tr("Please input the radius of the circle:"));
    lineEdit = new QLineEdit(this);
    label2 = new QLabel(this);
    button = new QPushButton(this);
    button->setText(tr("Display the area of the corresponding circle"));
    QGridLayout *mainLayout = new QGridLayout(this);
    mainLayout->addWidget(label1,0,0);
    mainLayout->addWidget(lineEdit,0,1);
    mainLayout->addWidget(label2,1,1);
    mainLayout->addWidget(button,1,0);
    connect(button,SIGNAL(clicked()),this,SLOT(showArea()));

}

circleArea::~circleArea()
{

}

void circleArea::showArea()
{
    bool ok;
    QString tempStr;
    QString valueStr = lineEdit->text();
    int valueInt = valueStr.toInt(&ok);
    double area = valueInt * valueInt * PI;
    label2->setText(tempStr.setNum(area));
}

       在这里我借用了影佬画的那只猫咪,这只猫咪被我运用在各种头像和图标上,哈哈哈!在此谢过影佬~刁肥宅起手!

       源文件main.cpp:

//main.cpp

#include "circlearea.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    circleArea w;
    
    QIcon icon("E:/documents/picture/miao.jpg");
    w.setWindowIcon(icon);
    w.setWindowTitle("CalculateCircleAreaBaseOnRadius");
    
    w.show();

    return a.exec();
}
图1 圆半径为5时求圆面积
图2 圆半径为10时求圆面积
图3 圆半径为99时求圆面积

猜你喜欢

转载自blog.csdn.net/u25th_engineer/article/details/81878005
今日推荐