why my custom signal not call the custom slot ?
-
i have made custom signal and slot below way :
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); void errorcodevalue (); signals: void mySignal(int myParameter); private slots: void on_send_button_clicked(); void on_pushButton_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_Hdefinition of this signal slot added in below file :
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked)); } MainWindow::~MainWindow() { delete ui; } void MainWindow::errorcodevalue() { int i = 0; if(i==0) emit mySignal(i); else qDebug()<<"signal call failed"; } void MainWindow::on_send_button_clicked() { qDebug()<<"signal call success"; } void MainWindow::on_pushButton_clicked() { errorcodevalue(); }so can any body tell me why my custom signal not get emitted on click on pushbutton of ui ?
why my slot also not get called ?
what change can make it work ?
-
connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked() ));
but you can use lambda function for this;
connect( this, &MainWindow::mySignal, this, &MainWindow::on_send_button_clicked );@JoeCFD said in why my custom signal not call the custom slot ?:
connect( this, &MainWindow::mySignal, this, &MainWindow::on_send_button_clicked );
Dear @JoeCFD ,
your first solution not works for me but second solution works fine. can you tell me why first solution not working ?
-
i have made custom signal and slot below way :
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); void errorcodevalue (); signals: void mySignal(int myParameter); private slots: void on_send_button_clicked(); void on_pushButton_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_Hdefinition of this signal slot added in below file :
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked)); } MainWindow::~MainWindow() { delete ui; } void MainWindow::errorcodevalue() { int i = 0; if(i==0) emit mySignal(i); else qDebug()<<"signal call failed"; } void MainWindow::on_send_button_clicked() { qDebug()<<"signal call success"; } void MainWindow::on_pushButton_clicked() { errorcodevalue(); }so can any body tell me why my custom signal not get emitted on click on pushbutton of ui ?
why my slot also not get called ?
what change can make it work ?
@Qt-embedded-developer
A little bit of coherence should solve your issue.you declare your signal with a parameter but not in connect:
connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked));your slot has no parameter anyway, should work if adding parenthesis
SLOT(on_send_button_clicked () ));Look at the console, i'm sure there are connection errors messages.
One would recomand you to use new syntax for connections instead.
Doesn't resolve your logic errors anyway :) -
@Qt-embedded-developer
A little bit of coherence should solve your issue.you declare your signal with a parameter but not in connect:
connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked));your slot has no parameter anyway, should work if adding parenthesis
SLOT(on_send_button_clicked () ));Look at the console, i'm sure there are connection errors messages.
One would recomand you to use new syntax for connections instead.
Doesn't resolve your logic errors anyway :)@mpergand said in why my custom signal not call the custom slot ?:
SLOT(on_send_button_clicked () ));
dear i have added below line but its not working
connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked () ));i am getting below warning :
while code runs:QMetaObject::connectSlotsByName: No matching signal for on_send_button_clicked()
QObject::connect: No such signal MainWindow::mySignal() in ../customsignalchange/mainwindow.cpp:17
QObject::connect: (sender name: 'MainWindow')
QObject::connect: (receiver name: 'MainWindow') -
@mpergand said in why my custom signal not call the custom slot ?:
SLOT(on_send_button_clicked () ));
dear i have added below line but its not working
connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked () ));i am getting below warning :
while code runs:QMetaObject::connectSlotsByName: No matching signal for on_send_button_clicked()
QObject::connect: No such signal MainWindow::mySignal() in ../customsignalchange/mainwindow.cpp:17
QObject::connect: (sender name: 'MainWindow')
QObject::connect: (receiver name: 'MainWindow')@Qt-embedded-developer said in why my custom signal not call the custom slot ?:
connect(this, SIGNAL(mySignal(int)), this, SLOT(on_send_button_clicked () )); -
@mpergand said in why my custom signal not call the custom slot ?:
SLOT(on_send_button_clicked () ));
dear i have added below line but its not working
connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked () ));i am getting below warning :
while code runs:QMetaObject::connectSlotsByName: No matching signal for on_send_button_clicked()
QObject::connect: No such signal MainWindow::mySignal() in ../customsignalchange/mainwindow.cpp:17
QObject::connect: (sender name: 'MainWindow')
QObject::connect: (receiver name: 'MainWindow')@Qt-embedded-developer
Why do you not use New Signal Slot Syntax? You would be doing yourself a favour. It is now a decade old anyway, and would prevent the problems you are having withSIGNAL/SLOT()macros.