~风介~ 发表于 2014-9-27 21:42:36

求圆面积1

本帖最后由 ~风介~ 于 2015-11-20 19:59 编辑

code:类名:CountCircleArea
基于:QDialog
备注:去掉勾选ui文件
====================
//countcirclearea.h

#ifndef COUNTCIRCLEAREA_H
#define COUNTCIRCLEAREA_H

#include <QDialog>


QT_BEGIN_NAMESPACE
class QLabel;
class QLineEdit;
class QPushButton;
QT_END_NAMESPACE


class CountCircleArea : public QDialog
{
    Q_OBJECT

public:
    CountCircleArea(QWidget *parent = 0);


private:
    QLabel *label1,*label2;
    QLineEdit *lineEdit1;
    QPushButton *button1;
private slots:
    void showArea();

};

#endif // COUNTCIRCLEAREA_H

========================
//main.cpp
#include "countcirclearea.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    CountCircleArea countcirclearea;
    countcirclearea.show();

    return a.exec();
}

===========================
//countcirclearea.cpp
#include "countcirclearea.h"
#include <QtWidgets>

const static double PI = 3.1416;

CountCircleArea::CountCircleArea(QWidget *parent)
    : QDialog(parent)
{

    label1 = new QLabel(tr("Input Radius:"));
    label2 = new QLabel;
    lineEdit1 = new QLineEdit(tr("I Love FishC.com!"));
    button1 = new QPushButton("Display Area");
    QObject::connect(button1,SIGNAL(clicked()),this,SLOT(showArea()));


    QGridLayout *mainLayout = new QGridLayout;
    mainLayout ->addWidget(label1,0,0);
    mainLayout ->addWidget(lineEdit1,0,1);
    mainLayout ->addWidget(label2,1,0);
    mainLayout ->addWidget(button1,1,1);
    setLayout(mainLayout);
}

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

screenshot:










大个的糖果 发表于 2014-11-1 07:48:33

858418616 发表于 2015-6-25 14:28:10

thank!
页: [1]
查看完整版本: 求圆面积1