PDA

View Full Version : Problem with tableview "randomly" scrolling to top



Tottish
16th February 2011, 13:18
Hey!

I've been developing a program with two custom listviews (custom views derived from QTableView) in it. They both read from the same model which is a derived QSortFilterProxyModel that in turn has a QSqlQueryModel as sourcemodel.

I've recently discovered that "sometimes" when the QSqlQueryModel is updated the listview "pops" all the way to the top.
The QSqlQueryModel is updated every 2 seconds in a separate thread against a local MySQL server.
How many updates it takes before the list "pops" varies from maybe 5 to 10 minutes. An interesting note is that if I'm running a virus scan the pop is much more prone to occur and the list seldom go a minute without popping to the top.
It doesn't seem to matter whether I use it intensely (clicking and scrolling around like a mad man) or just sit back in my chair and watch.

I realize it's impossible to tell what the error is providing there is many custom classes and loads of code. But I'm just wondering if this type of behavior is ringing a bell for anyone?
Any ideas on where to start looking?

Thank you!
/Tottish

Tottish
16th February 2011, 16:54
I'm thinking that this behavior might be caused by the tableviews trying to read from the source model (via the proxy-model) while the update-thread is writing to it. Does this make sense?
If so, can I block the QSqlQueryModel instance from being read while it is being updated from a different thread?
From what I've come to learn from the docs on QMutex and QReadWriteLock they are used to protect functions/methods rather than instances of objects, however I don't have a clear understanding of these.

Thanks
/Tottish

EDIT: Well, it seems that indeed was the problem. Though I couldn't manage to lock the model object I instead created a buffer model object so that the listviews can never attempt to read from the model while the update is being performed.
That did the trick but it seems like a rather boring solution.
Is there anything wrong with doing this? Would some kind of lock be preferable?
The model should never be more than about 300 rows and 30 columns so I'm guessing it shouldn't be too hard on the performance?

EDIT2: To further clarify: As it is set up right now, the QSqlQueryModels are private members of the updatethread class and sends pointers (to the listviews) to these via a signal that is emitted upon update complete.

Thanks
/Tottish