how to call slot of desired class by emitting signal from other class ?
-
i have written code like below:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTimer> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); QTimer tPolling; signals: void sigPollRequest(); public slots: void slotPollTimeout(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H -------------------- #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDateTime> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QObject::connect(&tPolling,&QTimer::timeout,this,&MainWindow::slotPollTimeout); tPolling.start(1000); } MainWindow::~MainWindow() { delete ui; } void MainWindow::slotPollTimeout() { emit sigPollRequest(); } --------------------- #ifndef FORM_H #define FORM_H #include <QWidget> namespace Ui { class Form; } class Form : public QWidget { Q_OBJECT public: explicit Form(QWidget *parent = nullptr); ~Form(); public slots: bool slotPollingRequested(); private: Ui::Form *ui; }; #endif // FORM_H ------------------- #include "form.h" #include "mainwindow.h" #include "ui_form.h" #include <QDebug> Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form) { ui->setupUi(this); MainWindow *ptr = new(MainWindow); QObject::connect(ptr,&MainWindow::sigPollRequest,this,&Form::slotPollingRequested); } Form::~Form() { delete ui; } bool Form::slotPollingRequested() { qDebug()<<"hi"; }
here from using timer i am emitting one signal named sigPollRequest . using this signal i want to call slot named slotPollingRequested.
Now when i am running my code my code is emitting signal but it not calls the slot slotPollingRequested.
why its not calling my slot ? what i need to write to resolve this ?
-
@Christian-Ehrlicher i have achieved goal. but not the way i want it. it is somewhat different way.
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } ----------- #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTimer> #include "form.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); QTimer tPolling; signals: void sigPollRequest(); public slots: void slotPollTimeout(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H ----------------- #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDateTime> #include "form.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); Form *frm = new Form(); QObject::connect(this,SIGNAL(sigPollRequest()),frm,SLOT(slotPollingRequested())); QObject::connect(&tPolling,&QTimer::timeout,this,&MainWindow::slotPollTimeout); tPolling.start(1000); } MainWindow::~MainWindow() { delete ui; } void MainWindow::slotPollTimeout() { emit sigPollRequest(); } --------------- #ifndef FORM_H #define FORM_H #include <QWidget> #include "mainwindow.h" namespace Ui { class Form; } class Form : public QWidget { Q_OBJECT public: explicit Form(QWidget *parent = nullptr); ~Form(); public slots: bool slotPollingRequested(); private: Ui::Form *ui; }; #endif // FORM_H ----------------- #include "form.h" #include "mainwindow.h" #include "ui_form.h" #include <QDebug> Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form) { ui->setupUi(this); } Form::~Form() { delete ui; } bool Form::slotPollingRequested() { qDebug()<<"hi"; }
But still have confusion that how i can call slot of form class by signal of mainwindow class . ultimately i want to write my connect system call into my form class and want to make it work.
@Qt-embedded-developer
Make yourself a rule: do not allow yourself to put#include "mainwindow.h"
anywhere other than inmainwindow.cpp
. No other class/module should know about or be able to reference it. So take that out ofform.h
/.cpp
. That you won't be tempted to do anything aboutclass MainWindow
there.MainWindow
createsForm *frm = new Form();
, soMainWindow
can connect a signal inForm
to a slot inMainWindow
, or the other way round, like:connect(form, &Form::signal, this, &MainWindow::slot); // or connect(this, &MainWindow::signal, form, &Form::slot);
This should be done in
MainWindow
class code, not inForm
class code. -
i have written code like below:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTimer> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); QTimer tPolling; signals: void sigPollRequest(); public slots: void slotPollTimeout(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H -------------------- #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDateTime> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QObject::connect(&tPolling,&QTimer::timeout,this,&MainWindow::slotPollTimeout); tPolling.start(1000); } MainWindow::~MainWindow() { delete ui; } void MainWindow::slotPollTimeout() { emit sigPollRequest(); } --------------------- #ifndef FORM_H #define FORM_H #include <QWidget> namespace Ui { class Form; } class Form : public QWidget { Q_OBJECT public: explicit Form(QWidget *parent = nullptr); ~Form(); public slots: bool slotPollingRequested(); private: Ui::Form *ui; }; #endif // FORM_H ------------------- #include "form.h" #include "mainwindow.h" #include "ui_form.h" #include <QDebug> Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form) { ui->setupUi(this); MainWindow *ptr = new(MainWindow); QObject::connect(ptr,&MainWindow::sigPollRequest,this,&Form::slotPollingRequested); } Form::~Form() { delete ui; } bool Form::slotPollingRequested() { qDebug()<<"hi"; }
here from using timer i am emitting one signal named sigPollRequest . using this signal i want to call slot named slotPollingRequested.
Now when i am running my code my code is emitting signal but it not calls the slot slotPollingRequested.
why its not calling my slot ? what i need to write to resolve this ?
@Qt-embedded-developer said in how to call slot of desired class by emitting signal from other class ?:
MainWindow *ptr = new(MainWindow); QObject::connect(ptr,&MainWindow::sigPollRequest,this,&Form::slotPollingRequested);
Because you create a new instace of MainWindow instead using the correct one - c++ basics.
-
@Qt-embedded-developer said in how to call slot of desired class by emitting signal from other class ?:
MainWindow *ptr = new(MainWindow); QObject::connect(ptr,&MainWindow::sigPollRequest,this,&Form::slotPollingRequested);
Because you create a new instace of MainWindow instead using the correct one - c++ basics.
so can you tell me what i need to write ?
because we know that by adding header file we can access the other class function. so can you tell me what mistake i done ? and give guidance that how to resolve with example ?
-
so can you tell me what i need to write ?
because we know that by adding header file we can access the other class function. so can you tell me what mistake i done ? and give guidance that how to resolve with example ?
@Qt-embedded-developer said in how to call slot of desired class by emitting signal from other class ?:
and give guidance that how to resolve with example ?
Sorry no - passing a pointer from one class to another is really the basics of c++.
-
@Qt-embedded-developer said in how to call slot of desired class by emitting signal from other class ?:
and give guidance that how to resolve with example ?
Sorry no - passing a pointer from one class to another is really the basics of c++.
means i need to create instance of form class into the Mainwindow class and after that this slot get call ?
-
means i need to create instance of form class into the Mainwindow class and after that this slot get call ?
@Qt-embedded-developer said in how to call slot of desired class by emitting signal from other class ?:
means i need to create instance of form class into the Mainwindow class and after that this slot get call ?
You have to create you Form class somewhere yes - how should it be shown and used otherwise?
-
@Qt-embedded-developer said in how to call slot of desired class by emitting signal from other class ?:
means i need to create instance of form class into the Mainwindow class and after that this slot get call ?
You have to create you Form class somewhere yes - how should it be shown and used otherwise?
@Christian-Ehrlicher i have achieved goal. but not the way i want it. it is somewhat different way.
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } ----------- #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTimer> #include "form.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); QTimer tPolling; signals: void sigPollRequest(); public slots: void slotPollTimeout(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H ----------------- #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDateTime> #include "form.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); Form *frm = new Form(); QObject::connect(this,SIGNAL(sigPollRequest()),frm,SLOT(slotPollingRequested())); QObject::connect(&tPolling,&QTimer::timeout,this,&MainWindow::slotPollTimeout); tPolling.start(1000); } MainWindow::~MainWindow() { delete ui; } void MainWindow::slotPollTimeout() { emit sigPollRequest(); } --------------- #ifndef FORM_H #define FORM_H #include <QWidget> #include "mainwindow.h" namespace Ui { class Form; } class Form : public QWidget { Q_OBJECT public: explicit Form(QWidget *parent = nullptr); ~Form(); public slots: bool slotPollingRequested(); private: Ui::Form *ui; }; #endif // FORM_H ----------------- #include "form.h" #include "mainwindow.h" #include "ui_form.h" #include <QDebug> Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form) { ui->setupUi(this); } Form::~Form() { delete ui; } bool Form::slotPollingRequested() { qDebug()<<"hi"; }
But still have confusion that how i can call slot of form class by signal of mainwindow class . ultimately i want to write my connect system call into my form class and want to make it work.
-
@Christian-Ehrlicher i have achieved goal. but not the way i want it. it is somewhat different way.
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } ----------- #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTimer> #include "form.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); QTimer tPolling; signals: void sigPollRequest(); public slots: void slotPollTimeout(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H ----------------- #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDateTime> #include "form.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); Form *frm = new Form(); QObject::connect(this,SIGNAL(sigPollRequest()),frm,SLOT(slotPollingRequested())); QObject::connect(&tPolling,&QTimer::timeout,this,&MainWindow::slotPollTimeout); tPolling.start(1000); } MainWindow::~MainWindow() { delete ui; } void MainWindow::slotPollTimeout() { emit sigPollRequest(); } --------------- #ifndef FORM_H #define FORM_H #include <QWidget> #include "mainwindow.h" namespace Ui { class Form; } class Form : public QWidget { Q_OBJECT public: explicit Form(QWidget *parent = nullptr); ~Form(); public slots: bool slotPollingRequested(); private: Ui::Form *ui; }; #endif // FORM_H ----------------- #include "form.h" #include "mainwindow.h" #include "ui_form.h" #include <QDebug> Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form) { ui->setupUi(this); } Form::~Form() { delete ui; } bool Form::slotPollingRequested() { qDebug()<<"hi"; }
But still have confusion that how i can call slot of form class by signal of mainwindow class . ultimately i want to write my connect system call into my form class and want to make it work.
@Qt-embedded-developer
Make yourself a rule: do not allow yourself to put#include "mainwindow.h"
anywhere other than inmainwindow.cpp
. No other class/module should know about or be able to reference it. So take that out ofform.h
/.cpp
. That you won't be tempted to do anything aboutclass MainWindow
there.MainWindow
createsForm *frm = new Form();
, soMainWindow
can connect a signal inForm
to a slot inMainWindow
, or the other way round, like:connect(form, &Form::signal, this, &MainWindow::slot); // or connect(this, &MainWindow::signal, form, &Form::slot);
This should be done in
MainWindow
class code, not inForm
class code.