qfedu-basic-level/day10/sumWidget/widget.cpp

57 lines
1.2 KiB
C++
Raw Permalink Normal View History

2023-06-26 19:00:04 +08:00
#include "widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
setWindowTitle("加法计数器");
setFixedSize(500,100);
QPushButton *sumBtn=new QPushButton("计算",this);
sumBtn->setFixedSize(100,50);
sumBtn->move(300,25);
le1 = new QLineEdit(this);
le2 = new QLineEdit(this);
le1->setFixedSize(100,50);
le2->setFixedSize(100,50);
le1->move(0,25);
le2->move(200,25);
lable1 = new QLabel(this);
lable1->setText("+");
lable1->setFixedSize(10,50);
lable1->move(145,25);
lable2 = new QLabel(this);
lable2->setText("计算结果");
lable2->setFixedSize(100,50);
lable2->move(400,25);
connect(sumBtn,&QPushButton::clicked,this,&Widget::sum);
// connect(sumBtn,&QPushButton::clicked,this,lable2->setText(QString::number(c)));
}
void Widget::sum(){
lable2->setText("点击");
QString n1 = le1->text();
QString n2 = le2->text();
qDebug() << n1 << " " << n2;
// 将文本转化为数值
int ret = n1.toInt()+n2.toInt();
QString str = QString::number(ret);
qDebug() << str;
lable2->setText(str);
}
Widget::~Widget()
{
}