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.
    alt text

    alt text

    .cpp file

    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.


  • Moderators

    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) named sayHello. It's a method (signal) in MainWindow class.

    QPushButton has a method (signal) named clicked(), so you can connect that. If you want to call pushed() 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 call replyHi 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
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.