Solved Signal and Slots are not Working in my project
-
I am using Qt for my end semester programming project. I was making a program to understand signal and slots after watching an introductory video on youtube.
I am uploading pictures of my .ui .cpp .h files so that you guys can understand what I am trying to do.
As you can see in application output that it says "no such signal sayhello" even though I have included in my .h file. Moreover, according to the code when I press pushbutton_2 then text of pushbutton should change from "Bye" to "aaa". But such thing also doesn't happen.
-
I think you misunderstand how signals/slots work.
in your connect statement the first param is an object emitting a signal and the second one is a method (signal) of that object's class. Class
QPushButton
doesn't have a method (signal) namedsayHello
. It's a method (signal) inMainWindow
class.QPushButton
has a method (signal) namedclicked()
, so you can connect that. If you want to callpushed()
when the button "Hi" is clicked then you connect like this:connect(ui->pushButton_2, &QPushButton::clicked, this, &MainWindow::pushed);
If you want the main window to emit
sayHello
when the button "Hi" is pressed and then in response to that you want to callreplyHi
you make two connections:connect(ui->pushButton_2, &QPushButton::clicked, this, &MainWindow::sayHello); // emits sayHello when the button is clicked connect(this, &MainWindow::sayHello, this, &MainWindow::replyHi); // calls replyHi when sayHello is emitted