PDA

View Full Version : The dataChanged Signal Not Performing as Expected



Corny
24th January 2019, 21:32
I have a situation that I require some additional insight to solve. I have elected not to post any code at this time because the whole program is rather large and bulky, and I think to isolate just the code involved would be time prohibitive. In any event, I’m not too sure the problem involves the actual code or, what I believe is more likely, that I’m missing some switch, understanding or knowledge about the Qt environment that may be affecting the situation.

The situation is that I have sub-classed QAbstractItemModel to create a model that uses a similar paradigm as the parent child relationship used in a tree model. The main window displays the values from the model in a treeView, and about half of those values are being constantly updated. The update is initiated by a QTimer in the main window code, that emits a signal which is connected to a slot in the model class, which after insuring the models data is updated, emits the dataChanged signal.

Some of the components in the model represent objects that are moving in a three dimensional space and the signal mentioned above is used to indicate that the next movement is to take place. Not only are the geographic coordinates displayed in the treeView, if the user has so selected, an additional window is shown that has a map as it’s background and the objects position is shown as an icon on that map. The class that computes the next position is the same class that initiates and controls the graphic representation on the map. Every instance of that class is moved to it’s own thread when it is instantiated. All communication between any instances of this class with the model, or any other component of the program is performed solely with signals and slots. On a bit of a different issue, is there a way to see a representation of this communication concerning signals and slots while debugging?

In any event, every class, function and operational item appears to work just fine. I have been able to prove that every instantiation of an object that computes location and controls movement is performing its tasks correctly and that it is on a different thread. The problem is that, even though the treeView should be showing certain elements updating 10 times a second in the main window display, the treeView appears to only update when you move the mouse directly over the treeView portion of the main display. Every time the mouse movement stops, even to change direction, or moves off the treeView portion of the display, any values that were updating, immediately stop doing so. :(

At this point, I can only assume that for some reason, the dataChanged signal is emitted whenever the mouse is moving over thetreeView and not emitted when I expect it to be. When examining the code, It looks as if it should be properly emitted at the correct time and I can’t even guess why the signal would only be emitted whenever the mouse is moving over the treeView. I’m hoping someone can provide some insight into my problem and I’d like to thank anyone in advance that does

ChristianEhrlicher
25th January 2019, 16:44
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.

d_stranz
25th January 2019, 17:29
I would guess your dataChanged() signal is emitted with a wrong parent or something.

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 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.

ChristianEhrlicher
25th January 2019, 18:13
If the model was emitting with the wrong indexes, it would fail whether or not the mouse was involved
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.

d_stranz
26th January 2019, 01:49
A mouse move triggers a repaint of the underlying cell

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.

ChristianEhrlicher
26th January 2019, 08:40
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)

Corny
28th January 2019, 15:53
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 if
are you properly protecting the model against simultaneous updates with mutexes or some other thread synchronization device? and 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.

I want to thank you both again for pointing me in a direction that may provide an answer.

Corny

d_stranz
28th January 2019, 17:27
I have not specifically included anything to insure thread synchronization

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.

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.

Corny
28th January 2019, 17:45
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.

d_stranz
29th January 2019, 18:41
Actually, it's top left and bottom right, inclusive. From the docs:


If the items are of the same parent, the affected ones are those between topLeft and bottomRight inclusive.

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 do not have the same parent, the behavior is undefined.

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.

Top left and bottom right also implies that the items changing are contiguous between those two limits.

Corny
29th January 2019, 19:12
Actually, it's top left and bottom right, inclusive.

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...

...would need to emit multiple dataChanged() signals if the items come from different parts of the tree.

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

d_stranz
30th January 2019, 17:15
Maya Posch has written a great book on C++ multithreading (https://www.amazon.com/dp/1787121704) 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.

Corny
30th January 2019, 22:52
So, it turns out that earlier in the development process, I forgot :o 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. :mad: 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.

d_stranz
31st January 2019, 20:27
I had written some of the data points directly to the items

Oops. Won't be the first time that's happened.

Maya Posch also has a great web site / blog with all sorts of interesting things, programming and beyond.

Corny
23rd April 2019, 16:34
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.

ChristianEhrlicher
23rd April 2019, 16:48
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.

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, {})

Corny
23rd April 2019, 17:03
I'm not sure how to respond to your reply other than that when emitting the signal without anything for the roles argument dataChanged(idx, idx2), I encounter the problems outlined above. When I actually emit using dataChanged(idx, idx2, QVector<int>()), I no longer experience those problems. The results have proven to be repeatable on multiple test runs, using both methods of emitting the signal.

I may have stated it in a manner that is not easily understood, but as noted, I have run multiple tests using both methods, and have repeatably achieved the exact same results for the equivalent method used. Is there something that I'm not seeing?

ChristianEhrlicher
23rd April 2019, 17:23
There must be another reason - from the C++ side it doesn't matter - see https://en.cppreference.com/w/cpp/language/default_arguments or a good C++ book.

Corny
23rd April 2019, 17:40
I don't disagree, that in C++ that's the way it works. However I am at a loss understanding what exactly is happening here. I am simply attempting to let anyone else who may run into a similar circumstance, know how I resolved the situation.

d_stranz
23rd April 2019, 21:49
I don't disagree, that in C++ that's the way it works.

As Christian points out, there is absolutely no difference between emitting dataChanged() with two arguments vs. with a third argument being an empty (but explicitly declared) QVector<int>(). Both versions should compile to exactly the same thing (with the compiler inserting the default argument in the first instance), so there is absolutely no way that a slot receiving the signal can tell how it was called in the source code. If you are seeing a difference in behavior if you build and run code written first one way and then the other with no other code changes and exactly the same runtime test conditions then I would also be at a loss to explain why.