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 between two classes

Signals and Slots between two classes

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 1.2k Views 1 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.
  • K Offline
    K Offline
    ktrsathish
    wrote on last edited by aha_1980
    #1

    I am facing problem on connecting the signals between two classes. I have create slots, signals and connected the same but signals are not received in slots. Could you please help me out to find the problem. Please find the code which I have implemented.

    Form.H

    #ifndef FORM_H
    #define FORM_H
    
    #include <QWidget>
    #include "test.h"
    
    namespace Ui {
    class Form;
    }
    
    class Form : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Form(QWidget *parent = nullptr);
        ~Form();
    
    
    signals:
        void valueChanged(int newValue);
    
    private slots:
        void on_button_clicked();
    
        void off_button_clicked();
    
    private:
        Ui::Form *ui;
        int value;
        Test obj;
    };
    
    #endif // FORM_H
    

    Test.h

    #ifndef TEST_H
    #define TEST_H
    
    #include <QtCore/qglobal.h>
    #if QT_VERSION >= 0x050000
    #include <QtWidgets/QWidget>
    #else
    #include <QtGui/QWidget>
    #endif
    
    class Test : public QWidget
    {
        Q_OBJECT
    
        public:
            explicit Test(QWidget *parent = nullptr);
            void EnableImage(int value);
    
    signals:
            void ValueChanged(int value);
    };
    
    #endif // TEST_H
    

    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include <QLabel>
    
    
    namespace Ui {
    class Widget;
    }
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    
    
    public:
        explicit Widget(QWidget *parent = nullptr);
        ~Widget();
    
        QLabel *Picture;
    
    public slots:
        void setValue(int value);
    
    public:
        Ui::Widget *ui;
    };
    
    #endif // WIDGET_H
    

    form.cpp

    #include "form.h"
    #include "ui_form.h"
    
    Form::Form(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Form)
    {
        ui->setupUi(this);
        value = 0;
        Test obj;
    }
    
    Form::~Form()
    {
        delete ui;
    }
    
    void Form::on_button_clicked()
    {
        value = 1;
        obj.EnableImage(value);
    }
    
    void Form::off_button_clicked()
    {
        value = 0;
        obj.EnableImage(value);
    }
    

    main.cpp

    #include "widget.h"
    #include "form.h"
    #include "test.h"
    #include <QApplication>
    #include <QObject>
    
    
    extern Widget w;
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        Widget w;
        Form f;
        Test t;
    
    
        w.show();
        f.show();
    
        // help show windows apart
        f.move(w.geometry().left() + 50,w.geometry().top() + 50);
    
        QObject::connect(&t, SIGNAL(valueChanged(int)),
                             &w, SLOT(setValue(int)));
    
    
        return a.exec();
    }
    

    test.cpp

    #include "test.h"
    
    Test::Test(QWidget *parent) : QWidget(parent)
    {
    
    }
    
    void Test::EnableImage(int value)
    {
        emit ValueChanged(value);
    }
    
    widget.cpp
    #include "widget.h"
    #include "ui_widget.h"
    
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
        Picture = ui->image;
        Picture->hide();
    
    }
    
    void Widget::setValue(int value)
    {
        if(value == 0)
        {
            Picture->hide();
        }
        else
        {
            Picture->show();
        }
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Can you use the new connection syntax?
      QObject::connect(&t, SIGNAL(valueChanged(int)),&w, SLOT(setValue(int))); should become QObject::connect(&t,&Test::valueChanged,&w,&Widget::setValue);

      "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

      K 1 Reply Last reply
      2
      • VRoninV VRonin

        Can you use the new connection syntax?
        QObject::connect(&t, SIGNAL(valueChanged(int)),&w, SLOT(setValue(int))); should become QObject::connect(&t,&Test::valueChanged,&w,&Widget::setValue);

        K Offline
        K Offline
        ktrsathish
        wrote on last edited by
        #3

        @VRonin no change. signals are not recieved in slots.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @ktrsathish said in Singals and Slots between two classes:

          Form::Form(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::Form)
          {
          ui->setupUi(this);
          value = 0;
          Test obj;
          }

          What should 'Test obj' do here?

          void Form::on_button_clicked()
          {
          value = 1;
          obj.EnableImage(value);
          }

          This is not the object you're doing your connect on.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          K 1 Reply Last reply
          5
          • Christian EhrlicherC Christian Ehrlicher

            @ktrsathish said in Singals and Slots between two classes:

            Form::Form(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::Form)
            {
            ui->setupUi(this);
            value = 0;
            Test obj;
            }

            What should 'Test obj' do here?

            void Form::on_button_clicked()
            {
            value = 1;
            obj.EnableImage(value);
            }

            This is not the object you're doing your connect on.

            K Offline
            K Offline
            ktrsathish
            wrote on last edited by
            #5

            @Christian-Ehrlicher My actual implementaion shall follow the above architecture. so I have written the sample code to work first.

            mrjjM 1 Reply Last reply
            0
            • K ktrsathish

              @Christian-Ehrlicher My actual implementaion shall follow the above architecture. so I have written the sample code to work first.

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

              @ktrsathish
              Hi
              Could you zip sample and upload via
              https://www.justbeamit.com/ ( single use :( )
              any other dropbox, Google drive, etc that can give a link
              with no registration required to get it.

              Code looks ok, but its easy to miss stuff due to all the scrolling.

              K 1 Reply Last reply
              0
              • mrjjM mrjj

                @ktrsathish
                Hi
                Could you zip sample and upload via
                https://www.justbeamit.com/ ( single use :( )
                any other dropbox, Google drive, etc that can give a link
                with no registration required to get it.

                Code looks ok, but its easy to miss stuff due to all the scrolling.

                K Offline
                K Offline
                ktrsathish
                wrote on last edited by
                #7

                @mrjj https://drive.google.com/file/d/1d0Cc86uItT6F-ZzpbWtreATYICApRG1U/view?ts=5bd73a93
                Please check the code and let me know the details.

                mrjjM 1 Reply Last reply
                0
                • K ktrsathish

                  @mrjj https://drive.google.com/file/d/1d0Cc86uItT6F-ZzpbWtreATYICApRG1U/view?ts=5bd73a93
                  Please check the code and let me know the details.

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

                  @ktrsathish
                  super !
                  but it asks permissions.
                  Can you remove that ?

                  K 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @ktrsathish
                    super !
                    but it asks permissions.
                    Can you remove that ?

                    K Offline
                    K Offline
                    ktrsathish
                    wrote on last edited by
                    #9

                    @mrjj https://drive.google.com/file/d/1d0Cc86uItT6F-ZzpbWtreATYICApRG1U/view?usp=sharing can you try now.

                    mrjjM 1 Reply Last reply
                    1
                    • K ktrsathish

                      @mrjj https://drive.google.com/file/d/1d0Cc86uItT6F-ZzpbWtreATYICApRG1U/view?usp=sharing can you try now.

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

                      @ktrsathish
                      Hi
                      In class Form, you have
                      private:
                      ..
                      Test obj;

                      and you use that to emit

                      void Form::on_ABS_ON_clicked()
                      {
                      value = 1;
                      obj.EnableImage(value);
                      }

                      BUT
                      the one you hook up in main is another test instance
                      and its not used to emit the signal.

                      Test t;
                       QObject::connect(&t, &Test::ValueChanged, &w, &Widget::setValue);
                      
                      t is not the one u use to emit signal.
                      
                      

                      So that is why all looks fine but nothing happens.
                      The actual emitter object is not connected up.

                      So it was as mr. @Christian-Ehrlicher said / suggested

                      K 1 Reply Last reply
                      5
                      • mrjjM mrjj

                        @ktrsathish
                        Hi
                        In class Form, you have
                        private:
                        ..
                        Test obj;

                        and you use that to emit

                        void Form::on_ABS_ON_clicked()
                        {
                        value = 1;
                        obj.EnableImage(value);
                        }

                        BUT
                        the one you hook up in main is another test instance
                        and its not used to emit the signal.

                        Test t;
                         QObject::connect(&t, &Test::ValueChanged, &w, &Widget::setValue);
                        
                        t is not the one u use to emit signal.
                        
                        

                        So that is why all looks fine but nothing happens.
                        The actual emitter object is not connected up.

                        So it was as mr. @Christian-Ehrlicher said / suggested

                        K Offline
                        K Offline
                        ktrsathish
                        wrote on last edited by
                        #11

                        @mrjj said in Signals and Slots between two classes:

                        Test t;
                        QObject::connect(&t, &Test::ValueChanged, &w, &Widget::setValue);

                        Thanks. It is solved.

                        1 Reply Last reply
                        0

                        • Login

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