简体   繁体   中英

Qt - problems with Qt SLOTs

I'm trying to learn Qt, and I have a problem with SLOT

#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
    ui.setupUi(this);

    QObject::connect(ui.Button_Quit, SIGNAL(clicked()), qApp, SLOT(quit())); // WORK

    QObject::connect(ui.AddEmployee, SIGNAL(clicked()), this, SLOT(changeText())); // DOESN'T WORK


}

void MainWindow::changeText()
{

    ui.Button_Quit->setText("TEST");

}

Why doesn't the 2nd slot work? Sorry for this newbie question, but I have trouble understanding the logic of this library. :-)

Like in the comment (Jesper Juhl), the macros work at runtime by resolving into strings.

You can use any one of the below ways (pass the address of the functions)

 //THIS WAY, YOU NEED AN ADDITIONAL FUNCTION buttonClicked
 connect(ui->pushButton, &QPushButton::clicked,this,&MainWindow::buttonClicked);

Using Lambdas:

 //THIS WAY, YOU DO NOT REQUIRE AN ADDITIONAL FUNCTION.
 connect(ui->pushButton, &QPushButton::clicked,[=]() { ui->pushButton->setText("TEST"); });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM