修改下列代码中的错误:#include "widget.h" #include "ui_widget.h" #include "QPushButton" #include "QLabel" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); QPushButton * button=new QPushButton("Change Label",this); QLabel * label=new QLabel("I'm a label",this); label->move(50,50); connect(button,&QPushButton::clicked,this,&Widget::changeLabelText(label)); } Widget::~Widget() { delete ui; } void Widget::changeLabelText(QLabel *label) { //QLabel *label=new QLabel("access from code",this); //label->move(50,50); if(label->text().compare("access from code")==0) { label->setText("I'm a label"); } else { label->setText("access from code"); } }
时间: 2023-12-29 09:03:34 浏览: 73
QT.zip_site:www.pudn.com
修改后的代码如下:
```
#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QLabel>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QPushButton * button=new QPushButton("Change Label",this);
QLabel * label=new QLabel("I'm a label",this);
label->move(50,50);
connect(button,&QPushButton::clicked,[=](){changeLabelText(label);});
}
Widget::~Widget()
{
delete ui;
}
void Widget::changeLabelText(QLabel *label)
{
if(label->text().compare("access from code")==0)
{
label->setText("I'm a label");
}
else
{
label->setText("access from code");
}
}
```
主要修改了connect函数中的参数,将slot函数改成了lambda表达式,并且去掉了函数中创建QLabel对象的代码。
阅读全文