上面是creator的ui界面 从左到右从上之下的命名
m_pEnterLabel m_pEnterLineEdit
m_pShowLabel m_pShowLineEdit
m_pCaculatePushButton
.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
public slots:
void CaculateSlot();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H```
.cpp
#include "dialog.h"
#include <QString>
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
setWindowTitle(QString::fromLocal8Bit("计算圆面积"));
connect(ui->m_pCaculatePushButton,SIGNAL(clicked()),this,SLOT(CaculateSlot()));
}
void Dialog::CaculateSlot()
{
const static double PI=3.14159;//声明静态常量PI
bool ok;
QString C_TempStr;
QString C_ValueStr=ui->m_pEnterLineEdit->text();
double C_ValueDou=C_ValueStr.toInt(&ok); //将字符串转换为double数值
double C_AreaDou = C_ValueDou*C_ValueDou*PI;
ui->m_pShowLineEdit->setText(C_TempStr.setNum(C_AreaDou));//把数据转化成字符串类型
}
Dialog::~Dialog()
{
delete ui;
}
参考Qt5 开发及实例(第三版) 陆文周老师的教材P14计算圆面积,将其补全,并加上了一点注释。