Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeView from QListView from QAbstractListModel
Qt 6.11 is out! See what's new in the release blog

QTreeView from QListView from QAbstractListModel

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 2.2k Views 2 Watching
  • 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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #1

    I'll try to keep this brief, as often I type in a lot, and then I don't get much response and I get sad/frustrated :(

    I need seem encouragement/putting on the right path/allaying of fears.

    Presently we have a JSON file with a flat list of records, which have a number of arbitrary attributes/fields/columns:

    [
      {
        ...,
        ...,
      },
      {
        ...,
        ...,
      },
      ...
    ]
    

    [We have a class for the rows ultimately derived from QAbstractListModel. I see that the whole element row with its columns/fields (i.e. class instance) is dumped into a single self._data[index.row()] = value. This architecture is not to be changed. I don't think this is relevant. Sorry for the digression. I'm learning as I go. So we have a list model with blobs for the items.]

    At present this is a displayed in a QListView. A data item presents some field in its blob for the list item label. That's fine.

    Now we want to change this a bit. The JSON items will be nested inside "categories", like:

    [
      { category1,
        [ {item1}, {item2}, ... ]
      },
      { category2,
        [ {item3}, {item4}, {item5}, ... ]
      },
    ]
    

    We want to present this to the user in a QTreeView, so that user can see items nested inside categories/sections.

    But QAbstractListModel, and for that matter QAbstractTableModel, admonishes me:

    Since the model provides a more specialized interface than QAbstractItemModel, it is not suitable for use with tree views [although it can be used to provide data to a QListView]; you will need to subclass QAbstractItemModel if you want to provide a model for that purpose.

    But I'm stuck with a model which is already derived from QAbstractListModel... :(

    I'm trying to get my (sore) head around what's going on here, and how I can proceed. I guess the issue is that the present list model --- whose elements' class is shared with other areas of code -- doesn't have any ability for parentage, that's why I can't treeview-ise it?

    What model type are you supposed to put a QTreeView on? If a QListView can use a QAbstractListModel, where is/why isn't there a QAbstractTreeModel class for a QTreeView?

    Is there any way I can force a square-peg QAbstractListModel into a round-hole QTreeView? :) Or a different widget if, say, I stipulated there will only ever be 2 levels: categories/sections at the top with a list of leaves as children?

    Like, say, in Qt Designer, the left-hand pane contains a list of widgets to drag from, just like we have at present, but it divides them up into categories/sections (Layouts, Spacers, ...) to help the user, that's just what we want.
    Screenshot from 2020-02-07 16-26-52.png
    I think someone called in an "accordion". (Is it an available Qt widget??) But I guess although the selectable items are a list, in order to categorise/section/group the "data model" cannot be just a list, it has to have "parentage" for the sections too?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Not a direct answer but are you looking for something like the QJsonModel project ?

      Beside that you have the Simple Tree Model example that should give you a good overview of what is needed to write a tree model.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      JonBJ 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Not a direct answer but are you looking for something like the QJsonModel project ?

        Beside that you have the Simple Tree Model example that should give you a good overview of what is needed to write a tree model.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @SGaist
        Thanks for that, especially the simple tree model as a sample.

        Not surprisngly, a quick search on that page shows they are using

        class TreeModel : public QAbstractItemModel
        

        and similarly class QJsonModel(QtCore.QAbstractItemModel) for the JSON.

        But I am saying the model I'm given is already a QAbstractListModel --- note the List --- and it is used as such elsewhere in other code. So changing it will be problematic. Hence I want an idea for "square-peg into round-hole" ;-)

        I don't imagine that will be forthcoming!

        Meanwhile, that "accordion" widget I show in my pic from Qt Creator/Designer, do you know if that is available as a Qt widget??

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Maybe a custom proxy model on top of it ?

          As for the widget, it's a QToolBox

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          JonBJ 1 Reply Last reply
          0
          • SGaistS SGaist

            Maybe a custom proxy model on top of it ?

            As for the widget, it's a QToolBox

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @SGaist

            Maybe a custom proxy model on top of it ?

            Yes, I wondered about that.

            As for the widget, it's a QToolBox

            Oohh! I wish the man page showed a pic of it, but if you say so. So that does not use a model/view. Ah, I see it doesn't hold my leaves, I have to split them up into what the "tabs" are.

            But QToolbox has a "current item", and I don't see it supporting expanding or contracting each tab separately. It's more like a QTabbedWidget for showing one tab at a time? That's not what the Qt toolbox is doing, is it? What tab/item is the "current" one? There isn't such a thing. Where is the expanding/contracting provided from? What single widget type are they using for each tab content? It might be derived via a QToolbox, but did they have to write all that functionality?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              The current item is the widget shown under the "tab".

              You can put a QListView in there to show what you want.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              JonBJ 1 Reply Last reply
              0
              • SGaistS SGaist

                The current item is the widget shown under the "tab".

                You can put a QListView in there to show what you want.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @SGaist
                I'm obviously not getting it, because I thought the "current item" was the current tab, like on a QTabWidget. The docs don't mention anything about expanding/contracting of the tabs, clicking on the expander icon at the left, which seems odd to me, as there should be an expand/contract signal etc. I guess I'll need to try it out to understand!

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  The name might be a bit misleading. The item is indeed similar to the tab of the QTabWidget.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  JonBJ 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    The name might be a bit misleading. The item is indeed similar to the tab of the QTabWidget.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @SGaist
                    This may be moot now, as my stakeholder has decided he wants the data to be visually presented in a QTreeView rather than a QToolbox. However it was good to learn about that widget, I may check itout just to look at it but not sure now :)

                    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