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 connect a signal from one class to a slot in another class ?

how to connect a signal from one class to a slot in another class ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtconnectivityqt5.15.2
11 Posts 4 Posters 1.1k 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.
  • P Offline
    P Offline
    Prakash08
    wrote on 18 Apr 2024, 05:23 last edited by
    #1

    I have two classes MainWindow & Model. My goal is to connet the emit() signal from MainWindow class to came_here() slot in Model class.

    //signal from MainWindow calss 
    
    signals:
        void eject();
    
    Model mod;
    
    connect(ui->pushButton,&QPushButton::clicked,this, &MainWindow::go_there);
    
    //connect function 
    QObject::connect(this,&MainWindow::eject,&mod,&Model::came_here);
    
    //slot which emits the signal from MainWindow class
    private slots:
    void MainWindow::go_there()
    {
        qDebug()<<"go there method executed ..";
        emit eject();
    }
    
    //slot in Model class 
    public slots:
    void Model::came_here()
    {
        qDebug()<<"Hi from Model class :)";
    }
    
    S 1 Reply Last reply 18 Apr 2024, 05:27
    0
    • P Prakash08
      18 Apr 2024, 05:23

      I have two classes MainWindow & Model. My goal is to connet the emit() signal from MainWindow class to came_here() slot in Model class.

      //signal from MainWindow calss 
      
      signals:
          void eject();
      
      Model mod;
      
      connect(ui->pushButton,&QPushButton::clicked,this, &MainWindow::go_there);
      
      //connect function 
      QObject::connect(this,&MainWindow::eject,&mod,&Model::came_here);
      
      //slot which emits the signal from MainWindow class
      private slots:
      void MainWindow::go_there()
      {
          qDebug()<<"go there method executed ..";
          emit eject();
      }
      
      //slot in Model class 
      public slots:
      void Model::came_here()
      {
          qDebug()<<"Hi from Model class :)";
      }
      
      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 18 Apr 2024, 05:27 last edited by
      #2

      @Prakash08 your code looks ok, although you seem to have mixed code from header file and source file.

      You can simplify this by connecting the signal from push button directly to your model:

      connect(ui->pushButton, &QPushButton::clicked, &mod, &Model::came_here);
      

      So, to sum up. This should be in header file (.h):

      signals:
          void eject();
      
      Model mod;
      

      And this in source file (.cpp), preferably somewhere early like in the constructor of your main window:

      connect(ui->pushButton, &QPushButton::clicked, &mod, &Model::came_here);
      

      (Z(:^

      P 1 Reply Last reply 18 Apr 2024, 06:22
      1
      • S sierdzio
        18 Apr 2024, 05:27

        @Prakash08 your code looks ok, although you seem to have mixed code from header file and source file.

        You can simplify this by connecting the signal from push button directly to your model:

        connect(ui->pushButton, &QPushButton::clicked, &mod, &Model::came_here);
        

        So, to sum up. This should be in header file (.h):

        signals:
            void eject();
        
        Model mod;
        

        And this in source file (.cpp), preferably somewhere early like in the constructor of your main window:

        connect(ui->pushButton, &QPushButton::clicked, &mod, &Model::came_here);
        
        P Offline
        P Offline
        Prakash08
        wrote on 18 Apr 2024, 06:22 last edited by
        #3

        @sierdzio thank you, it worked. but if i want to connect a custom signal to slot from other class is that possible ?

        J 1 Reply Last reply 18 Apr 2024, 06:23
        0
        • P Prakash08
          18 Apr 2024, 06:22

          @sierdzio thank you, it worked. but if i want to connect a custom signal to slot from other class is that possible ?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 18 Apr 2024, 06:23 last edited by
          #4

          @Prakash08 said in how to connect a signal from one class to a slot in another class ?:

          but if i want to connect a custom signal to slot from other class is that possible ?

          Of course it is possible, why should it not be possible?
          It doesn't matter whether the signal is your own custom signal...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          P 1 Reply Last reply 18 Apr 2024, 06:48
          0
          • J jsulm
            18 Apr 2024, 06:23

            @Prakash08 said in how to connect a signal from one class to a slot in another class ?:

            but if i want to connect a custom signal to slot from other class is that possible ?

            Of course it is possible, why should it not be possible?
            It doesn't matter whether the signal is your own custom signal...

            P Offline
            P Offline
            Prakash08
            wrote on 18 Apr 2024, 06:48 last edited by
            #5

            @jsulm And how to code that ?

            J J 2 Replies Last reply 18 Apr 2024, 06:50
            0
            • P Prakash08
              18 Apr 2024, 06:48

              @jsulm And how to code that ?

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 18 Apr 2024, 06:50 last edited by
              #6

              @Prakash08 let me chatgpt that for you:

              To connect a custom signal from one class to a slot in another class, you can follow a similar approach as you did with the built-in signals and slots. Here's how you can do it:

              In the header file of the class containing the custom signal (let's call it SenderClass), declare the custom signal:

              class SenderClass : public QObject
              {
                 Q_OBJECT
              
              signals:
                 void customSignal();
              };
              

              In the header file of the class containing the slot (let's call it ReceiverClass), declare the slot:

              class ReceiverClass : public QObject
              {
                 Q_OBJECT
              
              public slots:
                 void customSlot();
              };
              

              Then, in the implementation file of the ReceiverClass, define the custom slot:

              #include "receiverclass.h" // Include the header file of ReceiverClass
              
              void ReceiverClass::customSlot()
              {
                 qDebug() << "Custom slot executed";
              }
              

              Finally, connect the custom signal to the slot in your code:

              SenderClass sender;
              ReceiverClass receiver;
              
              // Connect the custom signal from SenderClass to the custom slot in ReceiverClass
              QObject::connect(&sender, &SenderClass::customSignal, &receiver, &ReceiverClass::customSlot);
              
              // Emit the custom signal from SenderClass
              emit sender.customSignal();
              

              With this setup, when the custom signal customSignal() is emitted from an instance of SenderClass, the slot customSlot() in an instance of ReceiverClass will be executed. Make sure to include the necessary header files and namespaces in your actual implementation.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              P 1 Reply Last reply 18 Apr 2024, 06:59
              0
              • P Prakash08
                18 Apr 2024, 06:48

                @jsulm And how to code that ?

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 18 Apr 2024, 06:51 last edited by
                #7

                @Prakash08 said in how to connect a signal from one class to a slot in another class ?:

                And how to code that ?

                In exact same way.
                What is the problem you face?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • J J.Hilk
                  18 Apr 2024, 06:50

                  @Prakash08 let me chatgpt that for you:

                  To connect a custom signal from one class to a slot in another class, you can follow a similar approach as you did with the built-in signals and slots. Here's how you can do it:

                  In the header file of the class containing the custom signal (let's call it SenderClass), declare the custom signal:

                  class SenderClass : public QObject
                  {
                     Q_OBJECT
                  
                  signals:
                     void customSignal();
                  };
                  

                  In the header file of the class containing the slot (let's call it ReceiverClass), declare the slot:

                  class ReceiverClass : public QObject
                  {
                     Q_OBJECT
                  
                  public slots:
                     void customSlot();
                  };
                  

                  Then, in the implementation file of the ReceiverClass, define the custom slot:

                  #include "receiverclass.h" // Include the header file of ReceiverClass
                  
                  void ReceiverClass::customSlot()
                  {
                     qDebug() << "Custom slot executed";
                  }
                  

                  Finally, connect the custom signal to the slot in your code:

                  SenderClass sender;
                  ReceiverClass receiver;
                  
                  // Connect the custom signal from SenderClass to the custom slot in ReceiverClass
                  QObject::connect(&sender, &SenderClass::customSignal, &receiver, &ReceiverClass::customSlot);
                  
                  // Emit the custom signal from SenderClass
                  emit sender.customSignal();
                  

                  With this setup, when the custom signal customSignal() is emitted from an instance of SenderClass, the slot customSlot() in an instance of ReceiverClass will be executed. Make sure to include the necessary header files and namespaces in your actual implementation.

                  P Offline
                  P Offline
                  Prakash08
                  wrote on 18 Apr 2024, 06:59 last edited by Prakash08
                  #8

                  @J-Hilk said in how to connect a signal from one class to a slot in another class ?:

                  SenderClass sender;
                  ReceiverClass receiver;

                  // Connect the custom signal from SenderClass to the custom slot in ReceiverClass
                  QObject::connect(&sender, &SenderClass::customSignal, &receiver, &ReceiverClass::customSlot);

                  // Emit the custom signal from SenderClass
                  emit sender.customSignal();

                  in which file these lines to be written ?

                  J J 2 Replies Last reply 18 Apr 2024, 07:01
                  0
                  • P Prakash08
                    18 Apr 2024, 06:59

                    @J-Hilk said in how to connect a signal from one class to a slot in another class ?:

                    SenderClass sender;
                    ReceiverClass receiver;

                    // Connect the custom signal from SenderClass to the custom slot in ReceiverClass
                    QObject::connect(&sender, &SenderClass::customSignal, &receiver, &ReceiverClass::customSlot);

                    // Emit the custom signal from SenderClass
                    emit sender.customSignal();

                    in which file these lines to be written ?

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 18 Apr 2024, 07:01 last edited by
                    #9

                    @Prakash08 Emit should be clear, right? You emit a signal where you want/need to emit it. Usually sender class is emitting its signal.
                    First two lines: whereever you need these classes. connect() - put it where you have access to both, sender and receiver.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • P Prakash08
                      18 Apr 2024, 06:59

                      @J-Hilk said in how to connect a signal from one class to a slot in another class ?:

                      SenderClass sender;
                      ReceiverClass receiver;

                      // Connect the custom signal from SenderClass to the custom slot in ReceiverClass
                      QObject::connect(&sender, &SenderClass::customSignal, &receiver, &ReceiverClass::customSlot);

                      // Emit the custom signal from SenderClass
                      emit sender.customSignal();

                      in which file these lines to be written ?

                      J Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 18 Apr 2024, 07:02 last edited by
                      #10

                      @Prakash08 my friend you need to pick up some courses on c++ and qt this is as basic as it gets

                      // SenderClass.h
                      #ifndef SENDERCLASS_H
                      #define SENDERCLASS_H
                      
                      #include <QObject>
                      
                      class SenderClass : public QObject
                      {
                          Q_OBJECT
                      
                      public:
                          explicit SenderClass(QObject *parent = nullptr);
                      
                      signals:
                          void customSignal();
                      };
                      
                      #endif // SENDERCLASS_H
                      
                      // SenderClass.cpp
                      #include "SenderClass.h"
                      
                      SenderClass::SenderClass(QObject *parent) : QObject(parent)
                      {
                      
                      }
                      
                      // ReceiverClass.h
                      #ifndef RECEIVERCLASS_H
                      #define RECEIVERCLASS_H
                      
                      #include <QObject>
                      #include <QDebug>
                      
                      class ReceiverClass : public QObject
                      {
                          Q_OBJECT
                      
                      public:
                          explicit ReceiverClass(QObject *parent = nullptr);
                      
                      public slots:
                          void customSlot();
                      };
                      
                      #endif // RECEIVERCLASS_H
                      
                      // ReceiverClass.cpp
                      #include "ReceiverClass.h"
                      
                      ReceiverClass::ReceiverClass(QObject *parent) : QObject(parent)
                      {
                      
                      }
                      
                      void ReceiverClass::customSlot()
                      {
                          qDebug() << "Custom slot executed";
                      }
                      
                      // main.cpp
                      #include <QCoreApplication>
                      #include "SenderClass.h"
                      #include "ReceiverClass.h"
                      
                      int main(int argc, char *argv[])
                      {
                          QCoreApplication a(argc, argv);
                      
                          SenderClass sender;
                          ReceiverClass receiver;
                      
                          // Connect the custom signal from SenderClass to the custom slot in ReceiverClass
                          QObject::connect(&sender, &SenderClass::customSignal, &receiver, &ReceiverClass::customSlot);
                      
                          // Emit the custom signal from SenderClass
                          emit sender.customSignal();
                      
                          return a.exec();
                      }
                      

                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      P 1 Reply Last reply 18 Apr 2024, 10:33
                      2
                      • J J.Hilk
                        18 Apr 2024, 07:02

                        @Prakash08 my friend you need to pick up some courses on c++ and qt this is as basic as it gets

                        // SenderClass.h
                        #ifndef SENDERCLASS_H
                        #define SENDERCLASS_H
                        
                        #include <QObject>
                        
                        class SenderClass : public QObject
                        {
                            Q_OBJECT
                        
                        public:
                            explicit SenderClass(QObject *parent = nullptr);
                        
                        signals:
                            void customSignal();
                        };
                        
                        #endif // SENDERCLASS_H
                        
                        // SenderClass.cpp
                        #include "SenderClass.h"
                        
                        SenderClass::SenderClass(QObject *parent) : QObject(parent)
                        {
                        
                        }
                        
                        // ReceiverClass.h
                        #ifndef RECEIVERCLASS_H
                        #define RECEIVERCLASS_H
                        
                        #include <QObject>
                        #include <QDebug>
                        
                        class ReceiverClass : public QObject
                        {
                            Q_OBJECT
                        
                        public:
                            explicit ReceiverClass(QObject *parent = nullptr);
                        
                        public slots:
                            void customSlot();
                        };
                        
                        #endif // RECEIVERCLASS_H
                        
                        // ReceiverClass.cpp
                        #include "ReceiverClass.h"
                        
                        ReceiverClass::ReceiverClass(QObject *parent) : QObject(parent)
                        {
                        
                        }
                        
                        void ReceiverClass::customSlot()
                        {
                            qDebug() << "Custom slot executed";
                        }
                        
                        // main.cpp
                        #include <QCoreApplication>
                        #include "SenderClass.h"
                        #include "ReceiverClass.h"
                        
                        int main(int argc, char *argv[])
                        {
                            QCoreApplication a(argc, argv);
                        
                            SenderClass sender;
                            ReceiverClass receiver;
                        
                            // Connect the custom signal from SenderClass to the custom slot in ReceiverClass
                            QObject::connect(&sender, &SenderClass::customSignal, &receiver, &ReceiverClass::customSlot);
                        
                            // Emit the custom signal from SenderClass
                            emit sender.customSignal();
                        
                            return a.exec();
                        }
                        
                        P Offline
                        P Offline
                        Prakash08
                        wrote on 18 Apr 2024, 10:33 last edited by
                        #11

                        @J-Hilk yeah, thank you !

                        1 Reply Last reply
                        0

                        1/11

                        18 Apr 2024, 05:23

                        • Login

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