#ifndef WIDGET_H
#define WIDGET_H
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QtGui/QWidget>
#include <QGridLayout>
#include <QString>
#include<QMessageBox>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
private:
QLabel *label1, *label2;
QLineEdit *lineEdit;
QPushButton *button;
signals: void ss(QString);
private slots:
void showArea();
void showBox(QString);
};
#endif // WIDGET_H
#include "widget.h"
const static double PI = 3.1416;
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
label1 = new QLabel(this);
label1->setText(QString::fromLocal8Bit("请输入圆的半径:"));
lineEdit = new QLineEdit(this);
label2 = new QLabel(this);
button = new QPushButton(this);
button->setText(QString::fromLocal8Bit("显示对应圆的面积"));
QGridLayout *mainLayout = new QGridLayout(this);
mainLayout->addWidget(label1,0,0);
mainLayout->addWidget(lineEdit,0,1);
mainLayout->addWidget(label2,1,0);
mainLayout->addWidget(button,1,1);
connect(button,SIGNAL(clicked()),this,SLOT(showArea()));
connect (lineEdit,SIGNAL(textChanged(QString)),this,SLOT(showArea()));
connect(this,SIGNAL(ss(QString)),this,SLOT(showBox(QString)));
}
Widget::~Widget()
{
}
void Widget::showArea()
{
bool ok;
QString tempStr;
QString valueStr = lineEdit->text();
int valueInt=valueStr.toInt(&ok);
double area=valueInt*valueInt*PI;
label2->setText(tempStr.setNum(area));
emit ss(QString::fromLocal8Bit("ShowArea执行了"));
}
void Widget::showBox(QString a)
{
QMessageBox msgBox;
msgBox.setText(a);
msgBox.exec();
}