PDA

View Full Version : Hierarchical disparate data structure and MVD (QStandardItemModel / QTreeView)



NameRakes
24th December 2016, 07:44
I am in the process of developing an application which will have a "model tree" that helps users navigate around the data (model) and do their task. My rendering of "my model tree" since the image didn't upload:

|--Collection1
| |--c1-1, etc.
|
|--Collection2
| |--c2-1, etc.
|


I have wired context sensitive menus to the model tree, albeit clunky, they seem to work. FYI, there are disparate data structures at different levels in the tree, but usually small in size. Except at the leaf node, where they can be large, like a long table with 100k rows and 3-6 columns. I have gone through most MVD (model/view/delegate) examples, worked out a few myself (helps a lot!).

I derive from QStandardItemModel / QTreeView for the model/view aspect of "my model tree". For my prototype I have assumed QString for each item so that I can use

model->data(Qt::DisplayRole).toString()
to create and display "my model tree". Now my 2-part question:

1. I believe that I cannot use QStandardItemModel to represent disparate hierarchical data structures, where each node has its own data structure, not just different data. In this case I should derive from QAbstractItemModel, right?

2. Continue to use QStandardItemModel / QTreeView, or their subclasses, for "my model tree" and context menus. But for the actions triggered by the context menus, I could subclass from QAbstractItemModel in order to handle complex data structures, large tables and their views. Am I heading in the right direction?

Thanks for your comments and help.

anda_skoa
24th December 2016, 14:36
A custom model gives you the widest freedom on how your data is internally structures, but writing a tree model is a bit of challenge when doing it for the first time.

it is probably still the better long term solution, but if you have a working solution with the standard item model, consider as a next step to just attach data to each of the nodes.
You just need your own roles and can attach any data that can be put into a QVariant.

Cheers,
_

d_stranz
24th December 2016, 19:18
If you can get a copy of Mark Summerfield's "Advanced Qt Programming" (https://smile.amazon.com/Advanced-Qt-Programming-Creating-Development/dp/0321635906) this has almost 60 pages on tree models, starting with using QStandardItemModel for trees and continuing into custom tree models. Even though the book is Qt4-based, MV hasn't changed all that much with Qt5.

Covers things like drag and drop, editing, selection, etc. I found it very useful when I was mapping from my own hierarchical data structure into a custom tree model.

NameRakes
24th December 2016, 22:53
Thanks for the suggestion on the book, will get it. Do you have suggestions on books/web resources for 2D plotting using Qt (or just go with what's available in Qt doc)?

I am aware of Qwt, qCustomPlot and the newly released Qt Charts 2.x. Even if I get started with one of these, would like to have a solid reference.

d_stranz
24th December 2016, 23:23
I used Qwt 5 for a long time, but I didn't move to Qwt 6 because Uwe made too many incompatible changes along with some unconventional API choices. It would have been too hard to port my code.

I paid for an early commercial license to QtCharts when they were first introduced. It makes attractive charts, but is completely impossible to customize if what comes out of the box doesn't exactly fit your needs. You can't add new series or chart types because all of the implementation details are in hidden private classes, and there are no virtual functions in the public interfaces that would allow you to override behavior. It's a terrible design and isn't at all Qt-like because it is so rigid.

QCustomPlot seems to have the most promise but I haven't investigated too deeply other than download the package and try some of the examples.

I eventually wrote my own data plotting widget which uses parts of Qwt (for axes mostly) and uses QGraphicsView for the main canvas, but it is limited to line and scatter plots. For my data visualization, I need to display scatter plots where each individual point has not only a position but can have a size and color different from other points. I also needed to feed data to the plot from a QAbstractItemModel through a proxy model that selects columns from a source model to be used for position, color, and size and which will update the plot in real time as the source model changes.

Probably at some point I will port over to QCustomPlot; it looks like the new 2.0.0 version can be customized to do most of what I am doing, plus I get the advantage of many more graph types.

NameRakes
24th December 2016, 23:39
Thanks for sharing your experience. I would need mostly line plots and scatter may be useful for some - engineering and scientific 2d plots. I don't have the complication of points being of different color, but would be interested in feeding data as it becomes available to the item model.

Will try out both Qwt and qCustomPlot, and also look at their object model before committing to one of them. Thanks again!

d_stranz
25th December 2016, 00:26
Now if there was only a comparable 3D plotting library... Qt3D is not it, and QwtPlot3d is based on ancient OpenGL and hasn't been ported to Qt5. There's VTK but the learning curve is like trying to climb up a cliff.

NameRakes
28th December 2016, 04:55
Update on qCustomPlot. Been playing with it for a couple of days now, and appears to have sufficient bells and whistles for 2D plotting, including its own layout system for many different graphing areas (nothing to do with the Qt layouts), selection triggered signals from the plotting canvass(?) / window, very good documentation similar to Qt, and what appears to be a well thought out object model. There is some mention of 3D plotting (in the future?), didn't pay attention to it, since my main app needs the 2D aspect.

Was able to wire my table view with 2D plots in a few hours (my point is qCustomPlot and its examples are easy to follow). I won't rule out VTK, if and when I have to expand my app to 3D.

Disclaimer: Didn't try out Qwt, nor am I a GUI person. I am more of engineering / scientific backend computation person, so treat my comments on GUI tools with a pinch of salt!

d_stranz
30th December 2016, 18:13
Disclaimer: Didn't try out Qwt, nor am I a GUI person. I am more of engineering / scientific backend computation person

Join the club. Unfortunately, once you start writing scientific software for a living, you'll find that your users don't care how you are doing the background computation (so long as you're doing it correctly) but care a whole lot about how you are displaying the results to them. You'll end up spending 80% of your time on tweaking the GUI.

NameRakes
3rd January 2017, 00:27
Join the club. Unfortunately, once you start writing scientific software for a living, you'll find that your users don't care how you are doing the background computation (so long as you're doing it correctly) but care a whole lot about how you are displaying the results to them. You'll end up spending 80% of your time on tweaking the GUI.

Yes, I know that feeling only too well. Glad that I am not alone. I looked into VTK / Paraview a few years back. I might revisit VTK even for 2D plotting, since expanding to 3D (if and when that happens) on the same platform must be naturally beneficial. Also it might be easier from a support and licensing point of view.