Custom QML component using QAbstractTableModel
-
I have data that makes sense to be modeled by a subclass of
QAbstractTableModel
.I need to visualize that data in a very specific way, so I would like to build a custom QML component. Without getting into the details, it should be relatively easy to visualize with a handful of basic controls and a
Canvas
item. When the subclass ofQAbstractTableModel
changes and emits its signal, I want the QML component to update/repaint. I'm envisioning something like, when the signal gets emitted from the model, my component loops over all rows in the tablemodel, draws the appropriate things on the canvas, updates some controls, etc. I'm looking to do something very similar to this tutorial, except implemented entirely in QML, not C++.I can't seem to find an example of anything like this. In every example I can find of
QAbstractListModel
orQAbstractTableModel
bound to a QML object, that QML object is always aListView
or aTableView
, and not a custom object composed in QML.Is there a way to do this? Thanks!
-
Would switching to a QAbstractListModel (you'll get roles instead of columns to define your data) and using a Repeater with a custom delegate work in your usecase?
Or do you need a QAbstractTableModel because the number of columns is not fixed?
-
@malocascio said in Custom QML component using QAbstractTableModel:
that QML object is always a ListView or a TableView, and not a custom object composed in QML.
That's because
*View
s already have the basic plumbing to connect to a model in place but nothing stops you from doing it from scratch. Just have a look at the signals emitted byQAbstractItemModel
and just design a component that react to those signals. There is nothing particularly unique about the model, it just communicates with signals as any otherQObject
-
Would switching to a QAbstractListModel (you'll get roles instead of columns to define your data) and using a Repeater with a custom delegate work in your usecase?
Whether it's a list or a table model doesn't matter much. The number of columns is fixed, so a list model (using roles instead of columns) is fine. This is actually a separate question I wanted to ask in another thread... I don't really understand the concept of "roles." I keep googling it to find more explanations but none of them make sense to me for some reason.
The bigger issue is that I don't think I understand how to use a Repeater for this, if what I really need to do is draw something on a canvas for each of the items in the list (or for each row in the table). Can a repeater be used that way?
That's because *Views already have the basic plumbing to connect to a model in place [...] just design a component that react to those signals.
I think that's the part I was having trouble verbalizing, thank you for attaching the right words to it!
Just to make sure I actually understand the methodology, let me type out a scenario and make sure it passes a sanity check. Let's say that I have my subclass of
QAbstractTableModel
, which, like all descendants ofQAbstractItemModel
, emits those signals. What I need to do is develop a QML component that connects to those signals. Here's where I'm a little weak in my understanding... It seems to me that there are two things I need to do:- Somehow declare a model for the component, where the model is some
QAbstractItemModel
object in the context of myQQmlApplicationEngine
- Declare
onDataChanged
for my custom component, and it will automatically be connected to thedataChanged
signal that gets emitted from thatQAbstractItemModel
object
Is that correct, and if so, how do I declare the model object for my custom component?
ListView
andTableView
do this through a delegate model, which, as I mentioned above, I'm not sure I know how to use to draw things on a canvas. - Somehow declare a model for the component, where the model is some