QML with CPP model - updating model from worker thread
-
hi
I implemented a model-view example.
model is CPP
view is QMLCurrently, I can update the model from the view.
I want to run a worker thread that receives data with UDP and updates the model.
Can I trigger the model update slot from the thread ?
For example - something like this
void CMyModel::updateModel(int row) { bool change = false; beginInsertRows(QModelIndex(), row, row); //logic change = true; endInsertRows(); if(change) emit dataChanged(); }
-
Hi,
Use signals and slots between your thread and the model.
-
@SGaist
I just want to be sure,
In case my MODEL is interacting with the GUI (QML) (GUI can send updates to the model (CPP)).
If I properly protected with locks, is it OK to update from another thread?" VIEW(QML) ---send update --- > MODEL(CPP) <---- send update --- UDP THREAD "
in my case, the data that I receive from UDP and send to the view is a one-way flow
but my question is to understand the general casehope I explained myself
tanks -
The "send to the view" part is not clear. The schema looks like you are passing through the model but your writing makes it look like you try to access the GUI directly from the thread.
So how exactly are you doing it ?
-
The "send to the view" part is not clear. The schema looks like you are passing through the model but your writing makes it look like you try to access the GUI directly from the thread.
So how exactly are you doing it ?
@SGaist
hi,the correct flow
" UDP THREAD --- updateModel ---> MODEL(CPP) ---- emit update ---> VIEW(QML) "The use case is
get data ( UDP)
process the data
update the model
emit the updated model changes to the GUI(I'm new to QML, and I do not exactly understand the property biding between CPP files to QML.)
** I will design the solution in more detail, the UDP thread is not going to do the processing,
it's just for the question **for example - monitoring data
VIEW - QML that shows tables, stacks, and graphs
MODEL - the data after some processing.
UDP thread - get monitored dataIs it ok to update the model that binds to QML from the UDP thread?
-
Don't modify the model directly from the thread. Use signals and slots to transfer the data to the model.