zoukankan      html  css  js  c++  java
  • 计算圆面积

     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()));
    } 
    内在的趣味,表面的繁琐
  • 相关阅读:
    改变,必须改变
    厦门四日
    再谈兴趣的重要性,人的差别及如何认识自我
    xcode svn checkout的项目无法真机运行解决办法
    [转]c的fopen()打开文件的模式,第二个参数
    cocos2dx 横板游戏触屏人物和背景移动 方法1
    简单的小球移动隐含的bug
    使用CCHttpRequest后要记得release(),否则内存泄漏
    资源路径问题 (ios平台)
    cocos2dx 横板游戏触屏人物和背景移动 方法2
  • 原文地址:https://www.cnblogs.com/data1213/p/10739463.html
Copyright © 2011-2022 走看看