[Qt-interest] Confused about dataChanged Signal

Andre Somers andre at familiesomers.nl
Fri Jul 2 13:07:11 CEST 2010


Hi Bill,

Op 2-7-2010 12:26, Bill Moo schreef:
> Hello again.
>
> According the the v4.6.3 docs this signal is emitted when an existing 
> item changes, in addition to that the implication is that there is no 
> requirement for a setData(...) function on a read only model so with 
> that in mind I'm of the opinion that all is taken care of internally 
> for this RO model.
Well... sort of. There is no requirement for a setData implementation if 
the model is not to be edited *through the model interface*. That is, 
basically allow your users to modify the data in the model in the view 
itself.
>
> However, I am seriously confused in that I never see any updates 
> appearing in the table view with one exception, and that only appears 
> when I click the view!
>
> As I understand it (which must be incorrectly) I was assuming that the 
> grid would automagically get updated when my thread modified the 
> member variables having connected my Model's dataChanged(...) signal 
> to my QTableView's dataChanged Slot.
First of all, magic does not exist in the world of computer science. All 
behaviour is coded, somewhere somehow. Also, you do not need to connect 
the signal to the slot explicitly. Using ::setModel on the view will 
take care of that.

If you create your own QAbstractItemModel (or one of it's subclasses), 
you have to remember that it is only a well-defined interface that views 
can use to display the data in your own underlying data structures. 
There is no magic involved. If you create such an interface, then it is 
also up to you to make sure that changes in the underlying data 
structures are made known to the item model, so that the model in turn 
can signal the connected views on these changes. Since the underlying 
data structure can be *anything*, there is no way QAbstractItemModel can 
know automatically that changes have occurred. In fact, that's a big 
part of the point of QAbstractItemModel, to signal such things to views 
in a pre-defined way.
>
> Before I launch the thread I have set the FileName (column 0) to say 
> "Processing File. Please wait..." however this change never appears in 
> the grid unless I click the view (which forces an update).
>
> Due to the sheer size of the files being processed the the line number 
> value changes a lot, and I can see this in the qDebug() output, but 
> the UI never reflects this?
>
> Of course I have to ask, because I am changing the values of these 
> variables does this then mean that my Model is in fact not RO but RW?
The model can only know about changes in the data, if it being told 
about it. I think I suggested that you have your Workers emit a signal 
on such changes, and have your model connect to those. Did you read and 
understand that suggestion? Did you do anything with that? It was mend 
to tackle exactly this issue: the view needs to be aware that the 
underlying data has changed. Once it is, it should signal this change in 
the pre-defined format by emitting the dataChanged() signal.

So, in short:

* add signals to the Worker class that tell the outside world about changes
* in your model, create a slot to catch that signal
* connect the two for every instance of Worker
* in your slot, determine what worker emitted the signal (I suggested 
using QSignalMapper, but there are other ways)
* then, see what row that corresponds to in your model
* emit the dataChanged signal in your model with the appropriate indices 
to tell your views about the changes.

André



More information about the Qt-interest-old mailing list