QT实现计算圆面积

实现界面:
在这里插入图片描述
.h

#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include<QLabel>
#include<QPushButton>
#include<QLineEdit>
class Dialog : public QDialog
{
    
    
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

    QLabel *label1,*label2;
    QPushButton *QP;
    QLineEdit *QTone;

private slots:
    void showArea();
};

#endif // DIALOG_H

.cpp

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

const static double PI=3.14;
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    
    
    label1=new QLabel(this);
    label1->setText("请输入圆的半径:");
    QTone = new QLineEdit(this);
    label2=new QLabel(this);
    QP=new QPushButton(this);
    QP->setText("显示圆的面积");

    QGridLayout *QG=new QGridLayout(this);
    QG->addWidget(label1,0,0);
    QG->addWidget(QTone,0,1);
    QG->addWidget(label2,1,0);
    QG->addWidget(QP,1,1);
    connect(QP,&QPushButton::pressed,this,&Dialog::showArea);
}
void Dialog::showArea()
{
    
    
    bool ok;
    QString tempstr;
    QString valuestr=QTone->text();//获取输入
    int valueint =valuestr.toInt(&ok);//将输入转换为整型
    double area = valueint*valueint*PI;//计算面积
    label2->setText(tempstr.setNum(area));//将整型转化为字符串并显示
}
Dialog::~Dialog()
{
    
    

}

猜你喜欢

转载自blog.csdn.net/weixin_47414687/article/details/115151400