Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to call slot of desired class by emitting signal from other class ?
Forum Updated to NodeBB v4.3 + New Features

how to call slot of desired class by emitting signal from other class ?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 412 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by
    #1

    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 EhrlicherC 1 Reply Last reply
    0
    • Q Qt embedded developer

      @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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #8

      @Qt-embedded-developer
      Make yourself a rule: do not allow yourself to put #include "mainwindow.h" anywhere other than in mainwindow.cpp. No other class/module should know about or be able to reference it. So take that out of form.h/.cpp. That you won't be tempted to do anything about class MainWindow there.

      MainWindow creates Form *frm = new Form();, so MainWindow can connect a signal in Form to a slot in MainWindow, 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 in Form class code.

      1 Reply Last reply
      4
      • Q Qt embedded developer

        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 EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        Q 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          @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.

          Q Offline
          Q Offline
          Qt embedded developer
          wrote on last edited by
          #3

          @Christian-Ehrlicher

          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 ?

          Christian EhrlicherC 1 Reply Last reply
          0
          • Q Qt embedded developer

            @Christian-Ehrlicher

            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 ?

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            Q 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              @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++.

              Q Offline
              Q Offline
              Qt embedded developer
              wrote on last edited by
              #5

              @Christian-Ehrlicher

              means i need to create instance of form class into the Mainwindow class and after that this slot get call ?

              Christian EhrlicherC 1 Reply Last reply
              0
              • Q Qt embedded developer

                @Christian-Ehrlicher

                means i need to create instance of form class into the Mainwindow class and after that this slot get call ?

                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                Q 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @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?

                  Q Offline
                  Q Offline
                  Qt embedded developer
                  wrote on last edited by Qt embedded developer
                  #7

                  @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.

                  JonBJ 1 Reply Last reply
                  0
                  • Q Qt embedded developer

                    @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.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #8

                    @Qt-embedded-developer
                    Make yourself a rule: do not allow yourself to put #include "mainwindow.h" anywhere other than in mainwindow.cpp. No other class/module should know about or be able to reference it. So take that out of form.h/.cpp. That you won't be tempted to do anything about class MainWindow there.

                    MainWindow creates Form *frm = new Form();, so MainWindow can connect a signal in Form to a slot in MainWindow, 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 in Form class code.

                    1 Reply Last reply
                    4

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved