码迷,mamicode.com
首页 > 其他好文 > 详细

计算圆面积

时间:2019-04-20 00:04:50      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:push   编辑   com   转换   static   控件   const   obj   slot   

 1 /*################### Dialog.h文件 ############*/
 2 
 3 #ifndef DIALOG_H
 4 #define DIALOG_H
 5 
 6 /*最终呈现窗体的类之基类*/
 7 #include <QDialog>
 8 
 9 /*控件对象相关的类*/
10 #include <QLabel>
11 #include <QLineEdit>
12 #include <QPushButton>
13 
14 /*定义Dialog类,用来定义最终呈现的窗体对象*/
15 class Dialog : public QDialog
16 {
17     /*
18     启动Qt元对象系统的一些特征,如支持信号和槽等,Q_OBJECT是一个宏,必须放在类定义的私有区。
19     */
20     Q_OBJECT
21 
22 public:
23     explicit Dialog(QWidget *parent = nullptr);
24     ~Dialog();
25 
26     /*定义控件对象*/
27 private:
28     QLabel *label1,*label2;  /*定义标签*/
29     QLineEdit *lineEdit;     /*输入编辑框*/
30     QPushButton *button;     /*按钮*/
31 
32     /*定义点击控件触发之后的执行函数*/
33 private slots:
34     void showArea();
35 };
36 
37 #endif // DIALOG_H
 1 /*******************  Dialog.cpp  ***********************/
 2 
 3 #include "dialog.h"
 4 #include <QGridLayout>
 5 
 6 const static double PI = 3.1416;
 7 
 8 /*构造Dialog对象*/
 9 Dialog::Dialog(QWidget *parent):QDialog(parent)
10 {
11     /*初始化控件,分配空间,存储数据*/
12     label1 = new QLabel(this);
13     label1->setText(tr("半径:"));
14     label2 = new QLabel(this);
15     lineEdit = new QLineEdit(this);
16 
17     button = new QPushButton(this);
18     button->setText(tr("面积:"));
19 
20     /*
21     布局管理器,将所有控件的位置固定。
22     头文件:<QGridLayout>
23     */
24     QGridLayout *mainLayout = new QGridLayout(this);
25 
26     mainLayout->addWidget(label1,0,0);
27     mainLayout->addWidget(lineEdit,0,1);
28 
29     mainLayout->addWidget(label2,1,0);
30     mainLayout->addWidget(button,1,1);
31 
32     /*
33      * 点击控件触发槽函数
34      * 触发的方式:clicked()
35      * 响应事件的方法:showArea()
36      */
37     connect(button,SIGNAL(clicked()),this,SLOT(showArea()));
38 }
39 
40 /*
41   类外定义响应点击的函数
42 */
43 void Dialog::showArea()
44 {
45     bool ok;
46     QString tempStr;
47     QString valueStr = lineEdit->text();   //获取控件录入数据,字符串
48     int valueInt = valueStr.toInt(&ok);    //转换为int类型
49     double area = valueInt*valueInt*PI;    //计算方法
50     label2->setText(tempStr.setNum(area)); //填充到新控件
51 }
52 
53 Dialog::~Dialog()
54 {
55     //释放new 对象
56     delete label1;
57     delete label2;
58     delete lineEdit;
59     delete button;
60 }
 1 /********************  main.cpp  ************************/
 2 
 3 #include "dialog.h"  //用户自定义控件
 4 #include <QApplication>  //这个头文件是应用程序必须的
 5 
 6 int main(int argc, char *argv[])
 7 {
 8     QApplication a(argc, argv);
 9     Dialog w;  /*创建Dialog对象*/
10     w.show();  /*调用show方法,呈现出来*/
11 
12     return a.exec(); /*进入循环,检查用户事件,比如点击控件,输入数据等*/
13 }

技术图片技术图片

/********************  Dialog.cpp  *********************/

//定义第二种触发方式

/*构造Dialog对象*/
Dialog::Dialog(QWidget *parent):QDialog(parent)
{

    ...
    /*
     * 点击控件触发槽函数
     * 触发的方式:textChanged(QString),每次修改lineEdit控件的值,都会触发调用showArea()方法
     * 响应事件的方法:showArea()
     */
    connect(lineEdit,SIGNAL(textChanged(QString)),this,SLOT(showArea()));
} 

 

计算圆面积

标签:push   编辑   com   转换   static   控件   const   obj   slot   

原文地址:https://www.cnblogs.com/data1213/p/10739463.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!