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. Signals and Slots within same Class
Forum Updated to NodeBB v4.3 + New Features

Signals and Slots within same Class

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 4.0k Views 3 Watching
  • 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.
  • R Offline
    R Offline
    reddy9pp
    wrote on last edited by
    #1

    Hello,

    I want to run the Signal-Slot connection within same class.

    For example:

    mainWindow.h

    public:
        void  A(QString data);
    signals:
        void dataReceived(QString data);
    private slots:
        void B(QString data);
    private:
        Ui::MainWindow *ui;
    

    mainWindow.cpp

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QObject::connect(this , SIGNAL(dataReceived(QString)),this, SLOT(B(QString)));
    
       QString value="some_text";
       A(value);  // Can I use this to emit signal ??
    
    }
    
    
    void MainWindow::A(QString data)
    {
        emit  dataReceived(data);
    }
    
    void MainWindow::B(QString data)
    {
        ui->label->setText(data);
    }
    

    How to change call the function which emits the signal & connects to Slot within the same class??

    Pl45m4P 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi and welcome to devnet,

      There's no event loop running at that point of your code hence the slot is not called.

      Sorry for the wrong pointer, I was thinking about a different use case involving further propagation of the signal.

      From the constructor you should call the slot directly.

      As @VRonin suggested below, check that you are not missing the Q_OBJECT macro.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      VRoninV 1 Reply Last reply
      2
      • R Offline
        R Offline
        reddy9pp
        wrote on last edited by reddy9pp
        #3

        Thanks for the reply.

        Normally, Can we do this signal-slot within the same class?
        Can you provide me an example?

        mrjjM 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Yes you can, but if you are only going to use that in your constructor then, there's no point.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • R Offline
            R Offline
            reddy9pp
            wrote on last edited by
            #5

            No, I dont want to call it from constructor.
            Please, show me the other method

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by SGaist
              #6

              What example are you expecting ? You can connect a signal to a slot within the same class.

              [edit: Removed wrong reference to event loop SGaist]

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • R reddy9pp

                Thanks for the reply.

                Normally, Can we do this signal-slot within the same class?
                Can you provide me an example?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @reddy9pp

                Hi
                Just to be sure its 100% clear
                You did it right - but since MainWindows is often used this way

                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    MainWindow w; <<<< created here and tries to signal itself
                    w.show();
                
                    return a.exec(); <<< however, it needs to be in here for it to work
                }
                

                In this case, emitting the signal to itself in the constructor will not do anything.

                But emitting in other functions after the window is shown will work as its then inside a.exec();

                1 Reply Last reply
                3
                • R reddy9pp

                  Hello,

                  I want to run the Signal-Slot connection within same class.

                  For example:

                  mainWindow.h

                  public:
                      void  A(QString data);
                  signals:
                      void dataReceived(QString data);
                  private slots:
                      void B(QString data);
                  private:
                      Ui::MainWindow *ui;
                  

                  mainWindow.cpp

                  MainWindow::MainWindow(QWidget *parent)
                      : QMainWindow(parent)
                      , ui(new Ui::MainWindow)
                  {
                      ui->setupUi(this);
                  
                      QObject::connect(this , SIGNAL(dataReceived(QString)),this, SLOT(B(QString)));
                  
                     QString value="some_text";
                     A(value);  // Can I use this to emit signal ??
                  
                  }
                  
                  
                  void MainWindow::A(QString data)
                  {
                      emit  dataReceived(data);
                  }
                  
                  void MainWindow::B(QString data)
                  {
                      ui->label->setText(data);
                  }
                  

                  How to change call the function which emits the signal & connects to Slot within the same class??

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by
                  #8

                  @reddy9pp

                  Ignoring the constructor thing, you don't even need function A to emit the signal. You can emit your custom signals everywhere you want (inside constructor makes no sense). So instead of calling A and passing the value to your public function first, you can just emit the signal and pass the QString as parameter.


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  R 1 Reply Last reply
                  1
                  • Pl45m4P Pl45m4

                    @reddy9pp

                    Ignoring the constructor thing, you don't even need function A to emit the signal. You can emit your custom signals everywhere you want (inside constructor makes no sense). So instead of calling A and passing the value to your public function first, you can just emit the signal and pass the QString as parameter.

                    R Offline
                    R Offline
                    reddy9pp
                    wrote on last edited by
                    #9

                    @Pl45m4

                    Could you provide an example code? I am bit confused

                    Pl45m4P 1 Reply Last reply
                    0
                    • R reddy9pp

                      @Pl45m4

                      Could you provide an example code? I am bit confused

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on last edited by Pl45m4
                      #10

                      @reddy9pp

                      Sure (it's not that complicated)

                      public:
                          // constructor
                          // destructor
                      
                          void aRandomFnctn(); 
                      
                      signals:
                          void dataReceived(QString data);
                      
                      private slots:
                          void processData(QString data);
                      
                      private:
                          Ui::MainWindow *ui;
                      


                      MainWindow::MainWindow(QWidget *parent)
                          : QMainWindow(parent)
                          , ui(new Ui::MainWindow)
                      {
                          ui->setupUi(this);
                          connect(this, SIGNAL( dataReceived(QString) ), this, SLOT( processData(QString) ) );
                          
                        // if you are using Qt5, you can use this syntax for signals and slots
                       // connect( this, &MainWindow::dataReceived, this, &MainWindow::processData);
                      
                      }
                      
                      
                      void MainWindow::aRandomFnctn();
                      {
                         // do stuff
                        // [ ... ]
                      
                         QString value ="TEXT";
                      
                       /// stuff
                       // even more stuff
                       // [ ... ]
                       // just emit / trigger you signal where and when you want to notify another class or instance inside same class
                          emit dataReceived(value); // -> slot "processData" is called afterwards
                          emit dataReceived("TEST"); // this is possible too
                      
                         // continue doing stuff
                      
                      }
                      
                      void MainWindow::processData(QString data)
                      {
                          // do something with your data here
                      }
                      

                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      2
                      • SGaistS SGaist

                        Hi and welcome to devnet,

                        There's no event loop running at that point of your code hence the slot is not called.

                        Sorry for the wrong pointer, I was thinking about a different use case involving further propagation of the signal.

                        From the constructor you should call the slot directly.

                        As @VRonin suggested below, check that you are not missing the Q_OBJECT macro.

                        VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by VRonin
                        #11

                        @SGaist said in Signals and Slots within same Class:

                        There's no event loop running at that point of your code hence the slot is not called.

                        ??? Qt::DirectConnection don't care about no loop afaik. OP original code looks good to me.

                        Make sure you have Q_OBJECT in your mainWindow.h

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        SGaistS 1 Reply Last reply
                        3
                        • VRoninV VRonin

                          @SGaist said in Signals and Slots within same Class:

                          There's no event loop running at that point of your code hence the slot is not called.

                          ??? Qt::DirectConnection don't care about no loop afaik. OP original code looks good to me.

                          Make sure you have Q_OBJECT in your mainWindow.h

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by SGaist
                          #12

                          @VRonin said in Signals and Slots within same Class:

                          @SGaist said in Signals and Slots within same Class:

                          There's no event loop running at that point of your code hence the slot is not called.

                          ??? Qt::DirectConnection don't care about no loop afaik. OP original code looks good to me.

                          Make sure you have Q_OBJECT in your mainWindow.h

                          You're completely right ! I have mixed that with another consequence of calling signals directly in a constructor especially when it should propagate further to other objects in the code base.

                          Though it should be working, I still do not recommend doing that as it may have unexpected consequence.

                          Fixed my original answer.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          2

                          • Login

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