点击(此处)折叠或打开
-
//dialog.h
-
#ifndef DIALOG_H
-
#define DIALOG_H
-
-
#include <QDialog>
-
-
class QLabel;
-
class QLineEdit;
-
-
namespace Ui {
-
class Dialog;
-
}
-
-
class Dialog : public QDialog
-
{
-
Q_OBJECT
-
-
public:
-
explicit Dialog(QWidget *parent = 0);
-
~Dialog();
-
-
private slots:
-
void showArea();
-
-
private:
-
Ui::Dialog *ui;
-
QLabel *label1, *label2;
-
QLineEdit *lineEdit;
-
QPushButton *button;
-
};
-
- #endif // DIALOG_H
点击(此处)折叠或打开
-
//dialog.cpp
-
#include "dialog.h"
-
#include "ui_dialog.h"
-
#include <QtGui>//QtGui模块定义了图形用户界面类
-
-
Dialog::Dialog(QWidget *parent) :
-
QDialog(parent),
-
ui(new Ui::Dialog)
-
{
-
ui->setupUi(this);
-
label1 = new QLabel(this);
-
label1->setText("Input the radius of a circle");
-
label2 = new QLabel(this);
-
lineEdit = new QLineEdit(this);
-
button = new QPushButton(this);
-
button->setText("the area of the circle");
-
//QGridLayout是一个网格布局管理器,将所有控件的位置固定
-
QGridLayout *mainlayout = new QGridLayout(this);
-
mainlayout->addWidget(label1, 0, 0);//将label1放在网格第0行0列的网格中
-
mainlayout->addWidget(lineEdit, 0, 1);//将lineEdit放在网格第0行1列的网格中
-
mainlayout->addWidget(label2, 1, 0);//将label2放在网格第1行0列的网格中
-
mainlayout->addWidget(button, 1, 1);//将button放在网格第1行1列的网格中
-
-
// setWindowTitle("Calculate the area of a circle");//对话框标题
-
// setFixedHeight(sizeHint().height());//设置对话框的理想(合理)高度
-
-
connect(button, SIGNAL(clicked()), this, SLOT(showArea()));
-
}
-
-
Dialog::~Dialog()
-
{
-
delete ui;
-
}
-
-
void Dialog::showArea()
-
{
-
bool ok;
-
QString tempStr;
-
QString valueStr = lineEdit->text();
-
-
int valueInt = valueStr.toInt(&ok);
-
double area = valueInt * valueInt * 3.1416;
-
label2->setText(tempStr.setNum(area));
- }
点击(此处)折叠或打开
-
//main.cpp
-
#include "dialog.h"
-
#include <QApplication>
-
-
int main(int argc, char *argv[])
-
{
-
QApplication a(argc, argv);
-
Dialog w;
-
w.show();
-
-
return a.exec();
- }

对话框结果处理之后(将dialog.cpp中24,25行的注释去掉)的结果:
