I have a class called Data, and this class has a function with a loop that reads data from a device. The loop never stops reading data.
For the sake of this example, lets say that class Data has the following code:
class Data : public QObject
{
Q_OBJECT
public:
explicit Data(QObject *parent = nullptr){
genData();
}
//the job of this function is to always get data
void genData(){
while(true){
m_number++;
//somehow keep updating the value in QML and keep doing it?
}
}
private:
int m_number = 0;
};
So, what I need to do is be able to display m_number in the main.qml and update the value in the QML UI every time it changes in C++. In this case, the loop is incrementing the value of m_number.
I have some ideas of what I have to do to make this work, but I'm not positive. 1. I know I have to run the function in a different thread so that it doesnt block the rest of the program, and I was able to accomplish that. 2. I know I have to implement Q_PROPERTY and I was able to implement it as well, but it only shows the first value in Data::m_number(the initialized value 0).
What I dont know is how to make everything interact together. I'm also not sure if there's anything else I need to implement. I'm sure there's a lot of things I'm not seeing, though. I've read the documentation but I was only able to understand up to the point I'm currently at.
I would appreciate help and sample code that anyone could provide me with. Thank you.