Signal is not read by the Class
-
@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.@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.
-
@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.
@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_) {} -
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.cppthat defines aDogclass:// 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
Dogclass with a constructor and abarkmethod. This is just a blueprint for creatingDogobjects, not an instance itself.Now, in another C++ file (let's call it
main.cpp), we can create multiple instances of theDogclass:// 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 theDogclass, each with its own name. Each instance (dog1anddog2) is independent of the other. They are both instances of theDogclass, 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 inmain.cpp).
Lets modify the example to include another class that takes a
Dogpointer and calls thebarkmethod. Let's create a new class calledPerson:// 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
Personclass, we have acommandmethod that takes a pointer to aDogobject and calls theDog'sbarkmethod.Now, let's modify our
main.cppto use this newPersonclass:// 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 aDoginstance (dog1) and aPersoninstance (person1). We then call thecommandmethod on thePersoninstance, passing in a pointer to theDoginstance. This results in thePersoninstance commanding theDoginstance to bark. I hope this helps! Let me know if you have any other questions. -
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.cppthat defines aDogclass:// 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
Dogclass with a constructor and abarkmethod. This is just a blueprint for creatingDogobjects, not an instance itself.Now, in another C++ file (let's call it
main.cpp), we can create multiple instances of theDogclass:// 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 theDogclass, each with its own name. Each instance (dog1anddog2) is independent of the other. They are both instances of theDogclass, 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 inmain.cpp).
Lets modify the example to include another class that takes a
Dogpointer and calls thebarkmethod. Let's create a new class calledPerson:// 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
Personclass, we have acommandmethod that takes a pointer to aDogobject and calls theDog'sbarkmethod.Now, let's modify our
main.cppto use this newPersonclass:// 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 aDoginstance (dog1) and aPersoninstance (person1). We then call thecommandmethod on thePersoninstance, passing in a pointer to theDoginstance. This results in thePersoninstance commanding theDoginstance to bark. I hope this helps! Let me know if you have any other questions.@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-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.
@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 theQObject::connectin the scope that instantiates your Link class -
@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 theQObject::connectin the scope that instantiates your Link class@J-Hilk like this??
Link::Link(MainWindow *mainWindow_): mainWindow(mainWindow_) { connect(mainWindow,&MainWindow::data_received,this,&Link::update); } -
@J-Hilk like this??
Link::Link(MainWindow *mainWindow_): mainWindow(mainWindow_) { connect(mainWindow,&MainWindow::data_received,this,&Link::update); } -
@J-Hilk I have another question which i have posted in another link..can you explain that too?
-
@J-Hilk I have another question which i have posted in another link..can you explain that too?
@Vijaykarthikeyan You could at least post that another link...
-
@Vijaykarthikeyan You could at least post that another link...
-
V Vijaykarthikeyan has marked this topic as solved on
