Sub-classed QSqlQueryModel...added custom fields...make them bind to height / source
-
I have sub-classed a QSqlQueryModel and read some values from an SQLite database. I use a QML TableView using delegates in TableViewStyle (header, row, item) to display the data on my screen, sort, filter, etc. I'd like to be able to click on a row and have it expand to show more detail and change that row's height dynamically.
I'm working hard to avoid refreshing the model calling setQuery(query()) as 1) it should not be needed as I don't need a database update in this circumstances and 2) it's harder than you think to return to the correct location after the model resets and goes to the first row (tried this saving contentY (not recommended) and saving the current row to restore post model refresh using positionViewAtRow()...both do not work well.
Thus, I decided to update my model.
My model tracks a QMap<int, bool> and a QMap<int, int> for two custom fields; viz. 1) Expanded (bool) and 2) Height (int). I've ensured that data() has been updated to ensure QVariant database returns and returns for these two custom fields. I believe data is changing properly in the model because even though I don't plan to show the custom / additional fields if I add them to the TableView just to check and you can see here they show up. In fact, they change immediately on clicking a row.
What's working is the text does indeed update as you can see on the 3rd row. What does not work is 1) row height did not change and 2) image source (up chevron) did not change. Now, if I scroll this off screen for ~10x rows and come back height and image are updated.
So I've succeeded in binding to the text field inside my TableView row delegate but not to the height or image source fields. I'm also fairly certain my model is providing the data as when I push the data to the view I can see the updates. So my questions are:
- How can I improve the property binding for this to work properly?
- I've read about rowHeightProvider() as well but does not seem to be present in Qt 5.15.2...why?
- I've read about TableView forceLayout() but this to does not seem to work...why?
Thanks for reviewing this and hopefully someone has some great ideas to share. The power of a custom model is impressive but I'm struggling on the view side!
Thanks,
-Rich