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. Signal is not read by the Class
Forum Updated to NodeBB v4.3 + New Features

Signal is not read by the Class

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 4 Posters 2.2k 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.
  • Christian EhrlicherC Christian Ehrlicher

    @Vijaykarthikeyan said in Signal is not read by the Class:

    connect(&mainWindow,&MainWindow::received,this,&Link::update)

    I doubt the second class has a reference to the first one. I would expect a pointer here. How do you pass the mainwindow instance to the other class?

    V Offline
    V Offline
    Vijaykarthikeyan
    wrote on last edited by
    #4

    @Christian-Ehrlicher Screenshot 2023-10-19 103823.png

    1 Reply Last reply
    0
    • J.HilkJ J.Hilk

      Based on this

      @Vijaykarthikeyan said in Signal is not read by the Class:

      accessible by the another class in another .cpp

      and this

      connect(&mainWindow,&MainWindow::received,this,&Link::update);

      let me believe you're missing some c++ basics and you ended up with 2 instances of your mainwindow class thinking they are one and the same instance.

      V Offline
      V Offline
      Vijaykarthikeyan
      wrote on last edited by
      #5

      @J-Hilk i have declared like :

      MainWindow mainWindow;
      

      in second class called Link

      jsulmJ 1 Reply Last reply
      0
      • V Vijaykarthikeyan

        @J-Hilk i have declared like :

        MainWindow mainWindow;
        

        in second class called Link

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #6

        @Vijaykarthikeyan said in Signal is not read by the Class:

        in second class called Link

        So, I guess @J-Hilk is right and you have now two instances of MainWindow. Your second class should not create MainWindow instance, instead pass a pointer to MainWindow to the second class.

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

        V 1 Reply Last reply
        0
        • jsulmJ jsulm

          @Vijaykarthikeyan said in Signal is not read by the Class:

          in second class called Link

          So, I guess @J-Hilk is right and you have now two instances of MainWindow. Your second class should not create MainWindow instance, instead pass a pointer to MainWindow to the second class.

          V Offline
          V Offline
          Vijaykarthikeyan
          wrote on last edited by
          #7

          @jsulm ok..i understand..
          now I have changed the second class constructor like this:
          Now,In header I have declared like this:

          private:
          MainWindow *mainWindow;
          

          and in cpp:

          Link::Link()
          {
              mainWindow = new MainWindow();
            connect(mainWindow,&MainWindow::received,this,&Link::update);
          }
          

          and the update function is like this:

          void Link::update()
          {
              datagram = mainWindow->get_data();
              qDebug()<<datagram;
          
              startTime = timer2.elapsed();
              emit ready_to_calculate(datagram);
          
          }
          

          But is only printing the debug in serial read method of 1st class like this:
          Data available
          Data available
          Data available
          Data available
          Data available
          Data available
          Data available
          Data available
          Data available
          Data available
          Data available
          Data available
          Data available
          Data available
          Data available
          Data available
          Data available

          jsulmJ 1 Reply Last reply
          0
          • V Vijaykarthikeyan

            @jsulm ok..i understand..
            now I have changed the second class constructor like this:
            Now,In header I have declared like this:

            private:
            MainWindow *mainWindow;
            

            and in cpp:

            Link::Link()
            {
                mainWindow = new MainWindow();
              connect(mainWindow,&MainWindow::received,this,&Link::update);
            }
            

            and the update function is like this:

            void Link::update()
            {
                datagram = mainWindow->get_data();
                qDebug()<<datagram;
            
                startTime = timer2.elapsed();
                emit ready_to_calculate(datagram);
            
            }
            

            But is only printing the debug in serial read method of 1st class like this:
            Data available
            Data available
            Data available
            Data available
            Data available
            Data available
            Data available
            Data available
            Data available
            Data available
            Data available
            Data available
            Data available
            Data available
            Data available
            Data available
            Data available

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @Vijaykarthikeyan said in Signal is not read by the Class:

            mainWindow = new MainWindow();

            Come on, you basically did not change anything - you still create a MainWindow instance in Link. WHY?!
            I wrote: "instead pass a pointer to MainWindow to the second class".

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

            V 1 Reply Last reply
            1
            • jsulmJ jsulm

              @Vijaykarthikeyan said in Signal is not read by the Class:

              mainWindow = new MainWindow();

              Come on, you basically did not change anything - you still create a MainWindow instance in Link. WHY?!
              I wrote: "instead pass a pointer to MainWindow to the second class".

              V Offline
              V Offline
              Vijaykarthikeyan
              wrote on last edited by
              #9

              @jsulm without that thing,it crashes like this:

              Screenshot 2023-10-19 110101.png

              jsulmJ 1 Reply Last reply
              0
              • V Vijaykarthikeyan

                @jsulm without that thing,it crashes like this:

                Screenshot 2023-10-19 110101.png

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #10

                @Vijaykarthikeyan You should learn basic C++.
                Of course it will crash if you do not initialise the pointer. That's why I wrote (now for the third time): "instead pass a pointer to MainWindow to the second class"...
                And please post code as text, not pictures.

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

                V 1 Reply Last reply
                3
                • jsulmJ jsulm

                  @Vijaykarthikeyan You should learn basic C++.
                  Of course it will crash if you do not initialise the pointer. That's why I wrote (now for the third time): "instead pass a pointer to MainWindow to the second class"...
                  And please post code as text, not pictures.

                  V Offline
                  V Offline
                  Vijaykarthikeyan
                  wrote on last edited by
                  #11

                  @jsulm Can u guide me to anyother link where I can completely learn that in Qt creator platform..

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • V Vijaykarthikeyan

                    @jsulm Can u guide me to anyother link where I can completely learn that in Qt creator platform..

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    @Vijaykarthikeyan said in Signal is not read by the Class:

                    Qt creator platform

                    Qt Creator is an IDE, no platform.
                    Searching by yourself for c++ beginners stuff in the net should not be that hard.

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

                    V 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @Vijaykarthikeyan said in Signal is not read by the Class:

                      Qt creator platform

                      Qt Creator is an IDE, no platform.
                      Searching by yourself for c++ beginners stuff in the net should not be that hard.

                      V Offline
                      V Offline
                      Vijaykarthikeyan
                      wrote on last edited by
                      #13

                      @Christian-Ehrlicher But,before this..i followed the same procedure for UDP communication.It worked fine..The data received from udp accessed by the second class..That's why I have tried like this.I have nothing changed a single line from the udp communciation..If the data accessed by second class in udp by this method, why not working in this serial communication,,That's why I have doubted baudrate.

                      jsulmJ 1 Reply Last reply
                      0
                      • V Vijaykarthikeyan

                        @Christian-Ehrlicher But,before this..i followed the same procedure for UDP communication.It worked fine..The data received from udp accessed by the second class..That's why I have tried like this.I have nothing changed a single line from the udp communciation..If the data accessed by second class in udp by this method, why not working in this serial communication,,That's why I have doubted baudrate.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        @Vijaykarthikeyan It is really not difficult to pass a pointer to MainWindow (or any other data) to your Link class:

                        class Link:
                        {
                        public:
                            Link(MainWindow *mainWindow_):
                                mainWindow(mainWindow_)
                            {}
                        

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

                        1 Reply Last reply
                        4
                        • J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #15

                          So, @Vijaykarthikeyan for your better understanding:

                          Here's a simple example to illustrate the difference between a C++ file and an instance of a class.

                          Let's say we have a C++ file named Dog.cpp that defines a Dog class:

                          // Dog.cpp
                          #include <iostream>
                          #include <string>
                          
                          class Dog {
                          public:
                              std::string name;
                          
                              Dog(std::string dogName) {
                                  name = dogName;
                              }
                          
                              void bark() {
                                  std::cout << name << " says: Woof!" << std::endl;
                              }
                          };
                          

                          In this file, we've defined a Dog class with a constructor and a bark method. This is just a blueprint for creating Dog objects, not an instance itself.

                          Now, in another C++ file (let's call it main.cpp), we can create multiple instances of the Dog class:

                          // main.cpp
                          #include "Dog.cpp"
                          
                          int main() {
                              Dog dog1("Fido");
                              Dog dog2("Rex");
                          
                              dog1.bark();  // Outputs: Fido says: Woof!
                              dog2.bark();  // Outputs: Rex says: Woof!
                          
                              return 0;
                          }
                          

                          In main.cpp, we've created two instances of the Dog class, each with its own name. Each instance (dog1 and dog2) is independent of the other. They are both instances of the Dog class, but they are separate objects with their own state (in this case, their names).

                          So, one C++ file (Dog.cpp) does not equate to one instance of an object/class. The C++ file contains the definition (or blueprint) of the class, and instances of that class can be created in any other part of the program (like in main.cpp).


                          Lets modify the example to include another class that takes a Dog pointer and calls the bark method. Let's create a new class called Person:

                          // Person.cpp
                          #include "Dog.cpp"
                          
                          class Person {
                          public:
                              std::string name;
                          
                              Person(std::string personName) {
                                  name = personName;
                              }
                          
                              void command(Dog* dog) {
                                  std::cout << name << " commands: ";
                                  dog->bark();
                              }
                          };
                          

                          In this Person class, we have a command method that takes a pointer to a Dog object and calls the Dog's bark method.

                          Now, let's modify our main.cpp to use this new Person class:

                          // main.cpp
                          #include "Person.cpp"
                          
                          int main() {
                              Dog dog1("Fido");
                              Person person1("Alice");
                          
                              person1.command(&dog1);  // Outputs: Alice commands: Fido says: Woof!
                          
                              return 0;
                          }
                          

                          In this modified main.cpp, we create a Dog instance (dog1) and a Person instance (person1). We then call the command method on the Person instance, passing in a pointer to the Dog instance. This results in the Person instance commanding the Dog instance to bark. I hope this helps! Let me know if you have any other questions.


                          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.

                          V 1 Reply Last reply
                          4
                          • J.HilkJ J.Hilk

                            So, @Vijaykarthikeyan for your better understanding:

                            Here's a simple example to illustrate the difference between a C++ file and an instance of a class.

                            Let's say we have a C++ file named Dog.cpp that defines a Dog class:

                            // Dog.cpp
                            #include <iostream>
                            #include <string>
                            
                            class Dog {
                            public:
                                std::string name;
                            
                                Dog(std::string dogName) {
                                    name = dogName;
                                }
                            
                                void bark() {
                                    std::cout << name << " says: Woof!" << std::endl;
                                }
                            };
                            

                            In this file, we've defined a Dog class with a constructor and a bark method. This is just a blueprint for creating Dog objects, not an instance itself.

                            Now, in another C++ file (let's call it main.cpp), we can create multiple instances of the Dog class:

                            // main.cpp
                            #include "Dog.cpp"
                            
                            int main() {
                                Dog dog1("Fido");
                                Dog dog2("Rex");
                            
                                dog1.bark();  // Outputs: Fido says: Woof!
                                dog2.bark();  // Outputs: Rex says: Woof!
                            
                                return 0;
                            }
                            

                            In main.cpp, we've created two instances of the Dog class, each with its own name. Each instance (dog1 and dog2) is independent of the other. They are both instances of the Dog class, but they are separate objects with their own state (in this case, their names).

                            So, one C++ file (Dog.cpp) does not equate to one instance of an object/class. The C++ file contains the definition (or blueprint) of the class, and instances of that class can be created in any other part of the program (like in main.cpp).


                            Lets modify the example to include another class that takes a Dog pointer and calls the bark method. Let's create a new class called Person:

                            // Person.cpp
                            #include "Dog.cpp"
                            
                            class Person {
                            public:
                                std::string name;
                            
                                Person(std::string personName) {
                                    name = personName;
                                }
                            
                                void command(Dog* dog) {
                                    std::cout << name << " commands: ";
                                    dog->bark();
                                }
                            };
                            

                            In this Person class, we have a command method that takes a pointer to a Dog object and calls the Dog's bark method.

                            Now, let's modify our main.cpp to use this new Person class:

                            // main.cpp
                            #include "Person.cpp"
                            
                            int main() {
                                Dog dog1("Fido");
                                Person person1("Alice");
                            
                                person1.command(&dog1);  // Outputs: Alice commands: Fido says: Woof!
                            
                                return 0;
                            }
                            

                            In this modified main.cpp, we create a Dog instance (dog1) and a Person instance (person1). We then call the command method on the Person instance, passing in a pointer to the Dog instance. This results in the Person instance commanding the Dog instance to bark. I hope this helps! Let me know if you have any other questions.

                            V Offline
                            V Offline
                            Vijaykarthikeyan
                            wrote on last edited by
                            #16

                            @J-Hilk ok.. I understand that instead of instantiating the class, we should pass the class member as a pointer to the 2nd class method. So that should be the correct procedure.

                            J.HilkJ 1 Reply Last reply
                            0
                            • V Vijaykarthikeyan

                              @J-Hilk ok.. I understand that instead of instantiating the class, we should pass the class member as a pointer to the 2nd class method. So that should be the correct procedure.

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #17

                              @Vijaykarthikeyan said in Signal is not read by the Class:

                              @J-Hilk ok.. I understand that instead of instantiating the class, we should pass the class member as a pointer to the 2nd class method. So that should be the correct procedure.

                              I can't quite sign that statement the way it is ....

                              Yes, if you do it that way, it will work.

                              What you "should" do is make the QObject::connect in the scope that instantiates your Link class


                              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.

                              V 1 Reply Last reply
                              2
                              • J.HilkJ J.Hilk

                                @Vijaykarthikeyan said in Signal is not read by the Class:

                                @J-Hilk ok.. I understand that instead of instantiating the class, we should pass the class member as a pointer to the 2nd class method. So that should be the correct procedure.

                                I can't quite sign that statement the way it is ....

                                Yes, if you do it that way, it will work.

                                What you "should" do is make the QObject::connect in the scope that instantiates your Link class

                                V Offline
                                V Offline
                                Vijaykarthikeyan
                                wrote on last edited by
                                #18

                                @J-Hilk like this??

                                Link::Link(MainWindow *mainWindow_): mainWindow(mainWindow_)
                                {
                                connect(mainWindow,&MainWindow::data_received,this,&Link::update);
                                }
                                
                                J.HilkJ 1 Reply Last reply
                                0
                                • V Vijaykarthikeyan

                                  @J-Hilk like this??

                                  Link::Link(MainWindow *mainWindow_): mainWindow(mainWindow_)
                                  {
                                  connect(mainWindow,&MainWindow::data_received,this,&Link::update);
                                  }
                                  
                                  J.HilkJ Offline
                                  J.HilkJ Offline
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by
                                  #19

                                  @Vijaykarthikeyan

                                  alt text


                                  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.

                                  V 1 Reply Last reply
                                  1
                                  • J.HilkJ J.Hilk

                                    @Vijaykarthikeyan

                                    alt text

                                    V Offline
                                    V Offline
                                    Vijaykarthikeyan
                                    wrote on last edited by
                                    #20

                                    @J-Hilk I have another question which i have posted in another link..can you explain that too?

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • V Vijaykarthikeyan

                                      @J-Hilk I have another question which i have posted in another link..can you explain that too?

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #21

                                      @Vijaykarthikeyan You could at least post that another link...

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

                                      V 1 Reply Last reply
                                      1
                                      • jsulmJ jsulm

                                        @Vijaykarthikeyan You could at least post that another link...

                                        V Offline
                                        V Offline
                                        Vijaykarthikeyan
                                        wrote on last edited by
                                        #22

                                        @jsulm https://forum.qt.io/post/775285

                                        1 Reply Last reply
                                        0
                                        • V Vijaykarthikeyan has marked this topic as solved on

                                        • Login

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