I would guess your dataChanged() signal is emitted with a wrong parent or something. You should check if you really emit with the correct QModelIndex.
I would guess your dataChanged() signal is emitted with a wrong parent or something. You should check if you really emit with the correct QModelIndex.
Corny (28th January 2019)
I don't think that would explain why it works correctly some of the time but not others. If the model was emitting with the wrong indexes, it would fail whether or not the mouse was involved. It sounds like under some circumstances, the view gets updated properly but other times not.I would guess your dataChanged() signal is emitted with a wrong parent or something.
I am wondering if this is an event loop and thread synchronization issue. @Corny - are you properly protecting the model against simultaneous updates with mutexes or some other thread synchronization device? Even if each item is running in its own thread and is responsible for computing its new position on receipt of the timer signal, these computations will not all occur simultaneously. The tree and map views must be able to lock out any changes to the model while these views are updating themselves.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
This is wrong. A mouse move triggers a repaint of the underlying cell (e.g. due to focus changes) so the new data is fetched. The effect definitely points to a wrong dataChanged() emit.If the model was emitting with the wrong indexes, it would fail whether or not the mouse was involved
Corny (28th January 2019)
My testing (setting a breakpoint in data() of my tree model) shows that a mouse move does not trigger a fetch of new data unless mouse tracking is enabled and the mouse passes over a tree item. If mouse tracking in not enabled or if the mouse is moving over an empty area of the tree view, only a mouse click or other action that triggers a repaint (eg. moving the window containing the tree view) will cause a data fetch.A mouse move triggers a repaint of the underlying cell
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
That's exactly what I said. And when you've a style which highlights the cell with the mouse focus the even every mouse move will update the underlying cell (e.g. fusion style on linux)
First I would like to apologize for a slow response, but I was sent out to the field at the end of last week so I'm just now seeing your replies.
I'd like to thank you both ChristianEhrlicher and d_stranz for the assistance
You had asked ifand I have not specifically included anything to insure thread synchronization. I will also re-examine the dataChanged() signal parameters and see if I can find anything similar to what you mention. I will be sure to report back what I find, if anything.are you properly protecting the model against simultaneous updates with mutexes or some other thread synchronization device?
I want to thank you both again for pointing me in a direction that may provide an answer.
Corny
d_stranz (28th January 2019)
It can be tricky. One of my co-workers notified me yesterday about just such a problem, where methods marked as "const" (i.e. presumably read-only) were in fact updating mutable data behind the scenes using a "lazy evaluation" scheme to avoid heavy processing until the last minute.I have not specifically included anything to insure thread synchronization
Under the wrong circumstances, a shared pointer to a single instance was being used by two different threads; both of them were calling the same "const" method, but because the data had changed earlier (through non-const methods), the first thread in invoked the update of the mutable data and then the second thread got inconsistent data because the first thread had already yet changed the flag that said "no update needed" before the update was completely done. The idea of deferring updates of the internal state until someone actually asked for them turned out to be a gotcha.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
That does sounds a bit tricky, but what I'm attempting is not really all that complex. I'm going through the code right now and my thought at this point is that I may have gotten a bit confused with the QAbstractModel class documentation. The requirement for the dataChanged signal indicates two QModelIndex values need to be passed to the function, one for the top left corner of the data area and one for the top right. Since I'm only addressing one point (or cell), I was passing the same index value in both arguments. I'm in the process of verifying if that is a valid or invalid way of addressing just one point right now.
Actually, it's top left and bottom right, inclusive. From the docs:
This implies that if only one item is changed, the topLeft and bottomRight indexes should be the same. But for a tree view, this is important:If the items are of the same parent, the affected ones are those between topLeft and bottomRight inclusive.
This implies that if multiple items in the tree are changing at the same time, then you would need to emit multiple dataChanged() signals if the items come from different parts of the tree.If the items do not have the same parent, the behavior is undefined.
Top left and bottom right also implies that the items changing are contiguous between those two limits.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Corny (29th January 2019)
Thanks, you're absolutely correct, my bust. And as you have noted, I have managed to verify that using the same index value for both values references the one data point as desired.
As to your point concerning...
not only is that key, but the fact that the data comes from multiple instantiations of an item, has me leaning towards your original assessment about using a mutex or some other form of thread blocking. As I'm only vaguely familiar with the subject, I've been attempting to study up on the subject, as well as more accurately define the timing of the model and its items.
Thanks again
Corny
Maya Posch has written a great book on C++ multithreading which includes examples using Qt, STL, and other threading implementations. I learned a lot from reading it and used it to solve more than one problem.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
So, it turns out that earlier in the development process, I forgot that I had written some of the data points directly to the items without using the virtual function setData() in the sub-classed QAbstactItemModel. Of course the model is the only place that I was emitting the dataChanged() signal.
It looks like a simple logic error was the problem. Thanks again for all the help with this, I've learned a lot. Also, I'll check out that book by Maya Posch that you mentioned.
Corny
P.S. I would have marked this thread as solved but don't see a means of doing so.
Oops. Won't be the first time that's happened.I had written some of the data points directly to the items
Maya Posch also has a great web site / blog with all sorts of interesting things, programming and beyond.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Though this thread is rather old, I’m posting this in the event someone else comes across the same situation. Even though I indicated that I thought I had found the problem. It wasn’t until a bit later that I figured out the real problem.
It turns out that the QAbstractItemModel documentation for dataChanged(), states among other things that...
[signal] void QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int> ())
...
The optional roles argument can be used to specify which data roles have actually been modified. An empty vector in the roles argument means that all roles should be considered modified. The order of elements in the roles argument does not have any relevance.
Based on other Qt documentation that shows a value assigned to an argument apparently indicating that it is an optional argument, and that I wanted to address all roles, I assumed (erroneously), that if you wanted to address all of the rolls the role argument could be an empty vector, or the role argument was optional. However, emitting the signal without a role argument appeared to work but proved to be problematic. Then, when I came across the QAbstractItemView documentation for the dataChanged() signal, it stated in part...
[virtual protected slot] void QAbstractItemView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int> ())
...
The roles which have been changed can either be an empty container (meaning everything has changed), or a non-empty container with the subset of roles which have changed.
Which I took to mean that the role argument itself is not optional and a container must be used as the argument, but the contents of the container can be empty if you want to address all roles.
Sending an empty container QVector<int> solved the problem.
This is nonsense - not passing the third argument will let the compiler add the default value for the third argument which is an empty QVector<int>() - so it's *exactly to same if you call dataChanged(idx, idx2, QVector<int>()) or dataChanged(idx, idx2) or dataChanged(idx, idx2, {})
Bookmarks