最簡單的Qt程式:根據使用者所輸入圓半徑計算圓面積

u25th_engineer發表於2018-08-20

       這兩天剛開始接觸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時求圓面積

 

相關文章