Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Custom QML component using QAbstractTableModel
QtWS25 Last Chance

Custom QML component using QAbstractTableModel

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qml componentsqtquick2qml types
4 Posts 3 Posters 746 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    malocascio
    wrote on last edited by
    #1

    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 of QAbstractTableModel 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 or QAbstractTableModel bound to a QML object, that QML object is always a ListView or a TableView, and not a custom object composed in QML.

    Is there a way to do this? Thanks!

    VRoninV 1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • M malocascio

        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 of QAbstractTableModel 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 or QAbstractTableModel bound to a QML object, that QML object is always a ListView or a TableView, and not a custom object composed in QML.

        Is there a way to do this? Thanks!

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        @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 *Views 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 by QAbstractItemModel 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 other QObject

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1
        • M Offline
          M Offline
          malocascio
          wrote on last edited by
          #4

          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 of QAbstractItemModel, 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 my QQmlApplicationEngine
          • Declare onDataChanged for my custom component, and it will automatically be connected to the dataChanged signal that gets emitted from that QAbstractItemModel object

          Is that correct, and if so, how do I declare the model object for my custom component? ListView and TableView 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.

          Thanks for the help, @GrecKo and @VRonin!

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved