Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1390638
  • 博文数量: 120
  • 博客积分: 182
  • 博客等级: 入伍新兵
  • 技术积分: 2278
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-19 16:31
文章分类

全部博文(120)

文章存档

2015年(12)

2014年(13)

2013年(40)

2012年(55)

分类: Windows平台

2013-10-10 21:20:24

用设计器Qt Designer实现计算圆面积,完成下图所示的功能(布局图):当用户输入一个一个圆半径之后,可以显示计算后的圆的面积值

控件的属性设置如下:
class                 text               object
QLabel              半径               radiusLabel
QLineEdit                               radiusLineEdit
QLabel               面积               areaLabel_1
QLabel                                   areaLabel_2
QPushButton           计算               countBtn

代码实现:

点击(此处)折叠或打开

  1. //dialog.h
  2. #ifndef DIALOG_H
  3. #define DIALOG_H

  4. #include <QDialog>

  5. namespace Ui {
  6. class Dialog;
  7. }

  8. class Dialog : public QDialog
  9. {
  10.     Q_OBJECT //该宏的作用是启动Qt元对象系统的一些特性(比如支持信号和槽等)
  11.              //它必须放到类定义的私有区

  12. public:
  13.     explicit Dialog(QWidget *parent = 0);
  14.     ~Dialog();

  15. private slots:
  16.     void on_countBtn_2_clicked();

  17. private:
  18.     Ui::Dialog *ui;
  19. };

  20. #endif // DIALOG_H

点击(此处)折叠或打开

  1. //dialog.cpp
  2. #include "dialog.h"
  3. #include "ui_dialog.h"
  4. #include <QString>

  5. const static double PI = 3.1416;

  6. Dialog::Dialog(QWidget *parent) :
  7.     QDialog(parent),
  8.     ui(new Ui::Dialog)
  9. {
  10.     ui->setupUi(this);
  11. }

  12. Dialog::~Dialog()
  13. {
  14.     delete ui;
  15. }

  16. void Dialog::on_countBtn_2_clicked()
  17. {
  18.     bool OK;
  19.     QString tempStr;
  20.     QString valueStr = ui->radiusLineEdit->text();
  21.     int valueInt = valueStr.toInt(&OK);
  22.     double area = valueInt * valueInt * PI;//计算圆面积
  23.     ui->areaLabel_2->setText(tempStr.setNum(area));
  24. }

点击(此处)折叠或打开

  1. //main.cpp
  2. #include "dialog.h"//包含了程序中要完成功能的Dialog类的定义,在Dialog类
  3. #include <QApplication>//在每个使用Qt图形化应用程序中都必须使用一个QApplication

  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication a(argc, argv);//a是这个程序的QApplication对象
  7.     Dialog w;//创建一个对话框对象,在该类中完成各种功能
  8.     w.show();//当创建一个窗口部件的时候,默认它是不可见的,必须调用show()函数来使它变成可见

  9.     return a.exec();//程序进入消息循环,等待可能输入进行响应。这里就是main()把控制权
  10.                     //转交给Qt,Qt完成事件处理工作,当应用程序退出时exec()的值就会返回
  11.                     //在exec()中,Qt接受并处理用户和系统的事件并且把它们传递给适当的窗口部件
  12. }
这种方式是通过触发按钮事件完成计算圆的面积的功能。
阅读(7634) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~