zoukankan      html  css  js  c++  java
  • CH01

    CH101_计算圆的面积

    ui 如下:

    方法一:在LineEdit中输入半径值,然后点击计算,在面积中显示结果:

     1 void Dialog::on_countBtn_clicked()   // 信号用的是clicked()  QAbstractButton
     2 {
     3     bool ok;  
     4     QString tempStr;
     5     QString valueStr = ui->radiusLineEdit->text();
     6     int valueInt = valueStr.toInt(&ok); //ok 传出参数 ,可以判断转换是否成功!
     7     qDebug()<<ok;
     8 
     9     double area = valueInt*valueInt*PI;  //PI 是个宏
    10     ui->areaLabel_2->setText(tempStr.setNum(area));
    11 }

    方法二:在LineEdit 中输入半径值,直接就在后面显示面积:

    void Dialog::on_radiusLineEdit_textChanged(const QString &arg1)  //信号用的是textChanged(QString)   QLineEdit 
    {
        bool ok;
        QString tempStr;
        QString valueString = ui->radiusLineEdit->text();
        int valueInt = valueString.toInt(&ok);
        double area = valueInt*valueInt*PI;
        ui->areaLabel_2->setText(tempStr.setNum(area));
        //qDebug()<<arg1; // 显示LineEdit 中的文本
    }

    CH102_不用ui 来创建窗口:

    方法一:在LineEdit 中输入,点击计算得到结果

    #include <QLabel>
    #include <QLineEdit>
    #include <QPushButton>
    
    #ifndef DIALOG_H
    #define DIALOG_H
    //Dialog.h
    #include <QDialog>
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        Dialog(QWidget *parent = 0);
        ~Dialog();
    private:
        QLabel * label1,*label2;
        QLineEdit * lineEdit;
        QPushButton * button;
    private slots:
        void showArea();
    };
    
    #endif // DIALOG_H
     1 //Dialog.cpp
     2 #include "dialog.h"
     3 #include <QGridLayout>
     4 
     5 #define PI 3.1416
     6 
     7 Dialog::Dialog(QWidget *parent)
     8     : QDialog(parent)
     9 {
    10     label1 = new QLabel(this);
    11     label1->setText(tr("请输入圆的半径: "));
    12     lineEdit = new QLineEdit(this);
    13     label2 = new QLabel(this);
    14     button = new QPushButton(this);
    15     button->setText(tr("显示对应的圆面积"));
    16 
    17     QGridLayout * mainLayout = new QGridLayout(this);
    18     mainLayout->addWidget(label1,0,0);
    19     mainLayout->addWidget(lineEdit,0,1);
    20     mainLayout->addWidget(label2,1,0);
    21     mainLayout->addWidget(button,1,1);
    22 
    23     connect(button,SIGNAL(clicked()),this,SLOT(showArea())); //  SIGNAL() 和 SLOT() 是QT定义的两   
    24}
    25 Dialog::~Dialog()
    26 {
    27 }
    28 void Dialog::showArea()
    29 {
    30     bool ok;
    31     QString tempStr;
    32     QString valueStr = lineEdit->text();
    33     int valueInt = valueStr.toInt(&ok);
    34     double area = valueInt*valueInt*PI;
    35     label2->setText(tempStr.setNum(area));
    36 }

    方法二:在LineEdit 中输入内容,直接得到结果:

    仅仅需要修改Dialog.cpp 中的连接信号和槽的connet() 就行了:

     1 //Dialog.cpp 中 的构造函数
     2 Dialog::Dialog(QWidget *parent)
     3     : QDialog(parent)
     4 {
     5     label1 = new QLabel(this);
     6     label1->setText(tr("请输入圆的半径: "));
     7     lineEdit = new QLineEdit(this);
     8     label2 = new QLabel(this);
     9     button = new QPushButton(this);
    10     button->setText(tr("显示对应的圆面积"));
    11 
    12     QGridLayout * mainLayout = new QGridLayout(this);
    13     mainLayout->addWidget(label1,0,0);
    14     mainLayout->addWidget(lineEdit,0,1);
    15     mainLayout->addWidget(label2,1,0);
    16     mainLayout->addWidget(button,1,1);
    17 
    18     connect(lineEdit,SIGNAL(textChanged(QString)),this,SLOT(showArea()));
    19 }
  • 相关阅读:
    Vue 2.x windows环境下安装
    VSCODE官网下载缓慢或下载失败 解决办法
    angular cli 降级
    Win10 VS2019 设置 以管理员身份运行
    XSHELL 连接 阿里云ECS实例
    Chrome浏览器跨域设置
    DBeaver 执行 mysql 多条语句报错
    DBeaver 连接MySql 8.0 报错 Public Key Retrieval is not allowed
    DBeaver 连接MySql 8.0报错 Unable to load authentication plugin 'caching_sha2_password'
    Linux系统分区
  • 原文地址:https://www.cnblogs.com/zach0812/p/11334025.html
Copyright © 2011-2022 走看看