update the data , not from the model
-
i have a model view and data.
the view is able to show an edit the data using the model as in all qt examples.the view and the model describe QTreeView
but the data can be also changed from outside the model .
say from a network message .it is clear that i dont want the part that changing the data from the network will know anything about the model or qt at all.
so how am i suppose to call model beginResetModel() and endResetModel() before and after the change?
-
i have a model view and data.
the view is able to show an edit the data using the model as in all qt examples.the view and the model describe QTreeView
but the data can be also changed from outside the model .
say from a network message .it is clear that i dont want the part that changing the data from the network will know anything about the model or qt at all.
so how am i suppose to call model beginResetModel() and endResetModel() before and after the change?
@orio said in update the data , not from the model:
it is clear that i dont want the part that changing the data from the network will know anything about the model or qt at all.
Then you will have a real problem. You need a way to know that the data has changed externally if it is the data for your model. Otherwise you are likely to have to refresh the whole model from the external data, e.g. on a timer or when the view requests an operation.
-
i have a model view and data.
the view is able to show an edit the data using the model as in all qt examples.the view and the model describe QTreeView
but the data can be also changed from outside the model .
say from a network message .it is clear that i dont want the part that changing the data from the network will know anything about the model or qt at all.
so how am i suppose to call model beginResetModel() and endResetModel() before and after the change?
@orio I'm not sure exactly what you're trying to do, but it's essential that you keep your model data current. As @JonB said, if you don't, you lose all the Qt magic that keeps your UI objects up to date, and you'll have to do it yourself.
I'm currently working on an app that sends and receives network messages. My various models have slots connected to the network manager's finished() signal, and performs the updates there. Simple to code, and very reliable.
-
@orio I'm not sure exactly what you're trying to do, but it's essential that you keep your model data current. As @JonB said, if you don't, you lose all the Qt magic that keeps your UI objects up to date, and you'll have to do it yourself.
I'm currently working on an app that sends and receives network messages. My various models have slots connected to the network manager's finished() signal, and performs the updates there. Simple to code, and very reliable.
my app receives network messages and can also edit and send them.
so when editting message through the gui i use the qt model that has pointer to the actual data.
sometime the editing of data is done by other component , that is not gui or qt related .
but i still need somehow that the qt model will be updates
-
my app receives network messages and can also edit and send them.
so when editting message through the gui i use the qt model that has pointer to the actual data.
sometime the editing of data is done by other component , that is not gui or qt related .
but i still need somehow that the qt model will be updates
@orio said in update the data , not from the model:
but i still need somehow that the qt model will be updates
Already answered, did you read my response above? Yes, you need to arrange to inform the Qt model that external updates have taken place. How else can it know? You have to find code/approach for that. Qt is not "magic", if you don't inform it data has changed it won't know. You seems to be saying you don't have a way of knowing what has happened and then asking for Qt to know, how do you suppose that would come about?
-
my app receives network messages and can also edit and send them.
so when editting message through the gui i use the qt model that has pointer to the actual data.
sometime the editing of data is done by other component , that is not gui or qt related .
but i still need somehow that the qt model will be updates
@orio said in update the data , not from the model:
sometime the editing of data is done by other component
OK, then it seems you have two choices:
- make that other component aware of your model, and have it manipulate the model through its public methods (add/set/delete items)
- have your other component send a signal to the model, with an argument that contains all necessary information to update the model.
The latter approach is the more "Qt" way of doing this, though it might be a little more involved.
Either way, though, as @JonB has said, you must explicitly update the model. It has no way of magically knowing what's going on outside its boundaries.
-
@orio said in update the data , not from the model:
sometime the editing of data is done by other component
OK, then it seems you have two choices:
- make that other component aware of your model, and have it manipulate the model through its public methods (add/set/delete items)
- have your other component send a signal to the model, with an argument that contains all necessary information to update the model.
The latter approach is the more "Qt" way of doing this, though it might be a little more involved.
Either way, though, as @JonB has said, you must explicitly update the model. It has no way of magically knowing what's going on outside its boundaries.
Sorry for the bad English, maybe i am not understood.
Qt is used only for the gui.
Model is a qt class .The data is a "clean" c++ class , it should not know that it is being modeled or know qt model class , it does not have signals.
I can somehow update the qt-model using an interface.
But the model ask for signals before and after editing .
I really dont understand how anyone else doesn't have this problem , all of you updating the data only through the qt model?
-
Sorry for the bad English, maybe i am not understood.
Qt is used only for the gui.
Model is a qt class .The data is a "clean" c++ class , it should not know that it is being modeled or know qt model class , it does not have signals.
I can somehow update the qt-model using an interface.
But the model ask for signals before and after editing .
I really dont understand how anyone else doesn't have this problem , all of you updating the data only through the qt model?
-
Sorry for the bad English, maybe i am not understood.
Qt is used only for the gui.
Model is a qt class .The data is a "clean" c++ class , it should not know that it is being modeled or know qt model class , it does not have signals.
I can somehow update the qt-model using an interface.
But the model ask for signals before and after editing .
I really dont understand how anyone else doesn't have this problem , all of you updating the data only through the qt model?
@orio said in update the data , not from the model:
I really dont understand how anyone else doesn't have this problem , all of you updating the data only through the qt model?
No, some people/some of the time update the data in the model directly, without going through the model (e.g.
setData()
). But then, as stated previously, you must have some means of letting the model/views know what underlying data has been changed, or at worst that (potentially) the whole of the model has been altered.I really don't understand how anyone cannot understand this is a requirement if you are going to use a Qt model against your own data!
I can somehow update the qt-model using an interface.
Well this is an improvement over earlier where you imply your outside data/code "will know anything about the model or qt at all" :)
But the model ask for signals before and after editing .
Don't know what this means to you. If you don't want your other code to know about Qt signals you can write, say as @jsulm says, some kind of very simple "wrapper" at the Qt side which your code calls and the wrapper does any signal emitting for you. But you still have to be prepared for your code to call this whenever it changes data directly.
-
@orio said in update the data , not from the model:
I really dont understand how anyone else doesn't have this problem , all of you updating the data only through the qt model?
No, some people/some of the time update the data in the model directly, without going through the model (e.g.
setData()
). But then, as stated previously, you must have some means of letting the model/views know what underlying data has been changed, or at worst that (potentially) the whole of the model has been altered.I really don't understand how anyone cannot understand this is a requirement if you are going to use a Qt model against your own data!
I can somehow update the qt-model using an interface.
Well this is an improvement over earlier where you imply your outside data/code "will know anything about the model or qt at all" :)
But the model ask for signals before and after editing .
Don't know what this means to you. If you don't want your other code to know about Qt signals you can write, say as @jsulm says, some kind of very simple "wrapper" at the Qt side which your code calls and the wrapper does any signal emitting for you. But you still have to be prepared for your code to call this whenever it changes data directly.
@JonB said in update the data , not from the model:
No, some people/some of the time update the data in the model directly, without going through the model (e.g. setData()
Hey Jon, this brings up an interesting point (one I'm struggling with in my other topic) -- who is responsible for calling setData()? From the Qt tutorial video, I was under the impression that this was needed when QML code was used to update the model. But after reading your post, it seems that the C++ model code must also call setData() when model in the data changes from an external source?
I have to admit it's a little confusing knowing when to call beginResetModel()/endResetModel() vs beginInsertRows()/addInsertRows(). I wish there were a tutorial that talked about this a little more in depth.
-
@JonB said in update the data , not from the model:
No, some people/some of the time update the data in the model directly, without going through the model (e.g. setData()
Hey Jon, this brings up an interesting point (one I'm struggling with in my other topic) -- who is responsible for calling setData()? From the Qt tutorial video, I was under the impression that this was needed when QML code was used to update the model. But after reading your post, it seems that the C++ model code must also call setData() when model in the data changes from an external source?
I have to admit it's a little confusing knowing when to call beginResetModel()/endResetModel() vs beginInsertRows()/addInsertRows(). I wish there were a tutorial that talked about this a little more in depth.
@mzimmers
Remember I know nothing about QML.Models provide
setData()
. The important thing is that willemit dataChanged(...)
, with the appropriate parameters, for the view and anything else to deal with. If you have other separate or internal code which alters the model's data, you don't have to callsetData()
, you can update the data structure and emit thedataChanged()
. And similarly for insert/delete rows.You would call
begin
/endInsert
/DeleteRows()
when you know, by whatever means, that row(s) have been inserted at a particular place.begin
/endResetModel()
allows for any change in the model: you don't know what/where, or there are lots of them. It causes the whole model to be invalidated and start again from scratch. Can be a bit "slow". -
@mzimmers
Remember I know nothing about QML.Models provide
setData()
. The important thing is that willemit dataChanged(...)
, with the appropriate parameters, for the view and anything else to deal with. If you have other separate or internal code which alters the model's data, you don't have to callsetData()
, you can update the data structure and emit thedataChanged()
. And similarly for insert/delete rows.You would call
begin
/endInsert
/DeleteRows()
when you know, by whatever means, that row(s) have been inserted at a particular place.begin
/endResetModel()
allows for any change in the model: you don't know what/where, or there are lots of them. It causes the whole model to be invalidated and start again from scratch. Can be a bit "slow".@JonB I'd like to pursue this a bit further. Though the OP didn't post any code, I suspect this will be relevant to his problem as well.
I understand that the setData() implementation must emit dataChanged(), but...from where is setData() called? In my app, it's not getting called at all (judging from the telltales I put in my code). Yet, one of my models updates the QML just fine, and another one doesn't.