Signal is not read by the Class
-
I have implemented serial port communication in Qt C++. It is receiving data successfully. Now,I want this data could be accessible by the another class in another .cpp file.
This is the small code snippet from port side .cpp file:void MainWindow::serial_read() { qDebug()<<"Data available"; if(serial->isWritable()){ data.clear(); if(serial->bytesAvailable()>0) { data.append(serial->readLine()); qDebug()<<data; emit data_received(); } } }
The problem is,the signal is correctly emitted in this 1st class but in the 2nd class .cpp file,it is not received..
this is the 2nd class file connecting line:
connect(&mainWindow,&MainWindow::received,this,&Link::update);
I have been stuck with this 2nd class part only. The signal is correclty emitted. Does the Baudrate affect this signal slot mechanism? I have set the baudrate to 115200.
-
@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_) {}
-
@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?
-
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.
-
-
@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.
-
@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 -
@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". -
@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. -
@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.
-
@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.cpp
that defines aDog
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 abark
method. This is just a blueprint for creatingDog
objects, not an instance itself.Now, in another C++ file (let's call it
main.cpp
), we can create multiple instances of theDog
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 theDog
class, each with its own name. Each instance (dog1
anddog2
) is independent of the other. They are both instances of theDog
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 inmain.cpp
).
Lets modify the example to include another class that takes a
Dog
pointer and calls thebark
method. 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
Person
class, we have acommand
method that takes a pointer to aDog
object and calls theDog
'sbark
method.Now, let's modify our
main.cpp
to use this newPerson
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 aDog
instance (dog1
) and aPerson
instance (person1
). We then call thecommand
method on thePerson
instance, passing in a pointer to theDog
instance. This results in thePerson
instance commanding theDog
instance to bark. I hope this helps! Let me know if you have any other questions. -
@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::connect
in the scope that instantiates your Link class -