Qt linedeit中数据的读取和计算

leble和linedeit的数据显示是相同的

【数据显示】

    connect(btn,&QPushButton::clicked,this,[=](){
        QString meijie;
        jieguo->setText(meijie.setNum(jisuan(line)));
        l2->setText(meijie.setNum(jisuanint(line)));
    });

【数据计算】

//获取lineedit或label中的数据并在linedeit中显示【一般在label中显示】
double MainWindow::jisuan(QLineEdit *l){
    bool ok;
    QString lsnum;
    lsnum=l->text();
    int num = lsnum.toInt(&ok);

    double zuizhongjieguo = num*num*3.14;
    return zuizhongjieguo;
}
int MainWindow::jisuanint(QLineEdit *l){
    bool ok;
    QString lsnum;
    lsnum=l->text();
    int num = lsnum.toInt(&ok);

    double zuizhongjieguo = num*num*3.14;
    return zuizhongjieguo;
}

Qt linedeit中数据的读取和计算_第1张图片

Qt linedeit中数据的读取和计算_第2张图片
【本节代码】

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include //布局管理器

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    setFixedSize(400,200);
    ui->setupUi(this);
    l1 = new QLabel(this);
    l1->setText("输入圆的面积:");//label类
    line = new QLineEdit(this);//这是一个编辑框
    //label可以用来显示数字此处决定用linedit显示结果
    l2 = new QLabel(this);
    jieguo = new QLineEdit(this);
    l2->setText("int类型结果");
    btn = new QPushButton(this);
    btn->setText("计算结果是");
    l1->move(0,0);
    line->move(100,0);
    btn->move(0,100);
    jieguo->move(100,100);
    l2->move(200,100);

    connect(btn,&QPushButton::clicked,this,[=](){
        QString meijie;
        jieguo->setText(meijie.setNum(jisuan(line)));
        l2->setText(meijie.setNum(jisuanint(line)));
    });


}
//获取lineedit或label中的数据并在linedeit中显示【一般在label中显示】
double MainWindow::jisuan(QLineEdit *l){
    bool ok;
    QString lsnum;
    lsnum=l->text();
    int num = lsnum.toInt(&ok);

    double zuizhongjieguo = num*num*3.14;
    return zuizhongjieguo;
}
int MainWindow::jisuanint(QLineEdit *l){
    bool ok;
    QString lsnum;
    lsnum=l->text();
    int num = lsnum.toInt(&ok);

    double zuizhongjieguo = num*num*3.14;
    return zuizhongjieguo;
}

MainWindow::~MainWindow()
{
    delete ui;
}



你可能感兴趣的:(QT,qt,ui,开发语言)