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. Tree View with varying column counts
QtWS25 Last Chance

Tree View with varying column counts

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtreeviewtreemodelmodelmodel-view
5 Posts 3 Posters 2.1k 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.
  • D Offline
    D Offline
    DaveK 0
    wrote on 3 Sept 2018, 21:33 last edited by
    #1

    I would like to show in a QTreeView a model that has two levels of items. The child and parent items would have a different qty of columns and different column names. The parent items will all have 4 columns. Each parent item would have several child items with 5 columns that have no relation to the parent's columns.

    Below is a quick sketch of roughly how I would want it to appear. The parent columns show information about shipments between warehouses, the child items represent actual lines of inventory that make up that shipment. In this example "Shipment #3" and Shipment #5" are expanded to show child items.

    -Can this be achieved with a QTreeView?
    -Could I use a standard model, or do I need to create my own?
    -Do you know of any other similar examples on how I might implement this, or have any suggestions?

    0_1536010020120_70e44cca-7640-43a1-8a04-aacdf2388de1-image.png

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hieuht
      wrote on 23 Jun 2023, 10:04 last edited by
      #2

      I need to implement something like this too, does any one know the solution?

      J 1 Reply Last reply 23 Jun 2023, 10:21
      0
      • H hieuht
        23 Jun 2023, 10:04

        I need to implement something like this too, does any one know the solution?

        J Offline
        J Offline
        JonB
        wrote on 23 Jun 2023, 10:21 last edited by JonB
        #3

        @hieuht
        Just to get you going, if nobody provides a more specific example.

        A tree model/view allows for any number of columns varying per parent per int QAbstractItemModel::columnCount(const QModelIndex &parent = QModelIndex()) const. So your case of all children having 5 columns is permissible. I don't about your horizontal header for each parent-children, that is not supported, you would presumably need some custom delegate to handle that.

        I think you would need a QTreeView and your own model, because for a QTreeWidget you would not have access to the model to alter the column counts per parent.

        H 1 Reply Last reply 24 Jun 2023, 02:44
        1
        • J JonB
          23 Jun 2023, 10:21

          @hieuht
          Just to get you going, if nobody provides a more specific example.

          A tree model/view allows for any number of columns varying per parent per int QAbstractItemModel::columnCount(const QModelIndex &parent = QModelIndex()) const. So your case of all children having 5 columns is permissible. I don't about your horizontal header for each parent-children, that is not supported, you would presumably need some custom delegate to handle that.

          I think you would need a QTreeView and your own model, because for a QTreeWidget you would not have access to the model to alter the column counts per parent.

          H Offline
          H Offline
          hieuht
          wrote on 24 Jun 2023, 02:44 last edited by
          #4

          Hi @JonB,

          Thank you for your reply. I'm using QTreeView and I can set the number of column by columCount method. But it is quite limited.

          First, the child's count cannot more than the parent's count.
          Second, the child's column size depend on the parent's size like the following picture.

          a45f1087-02a6-4a89-942e-26f8c9e6371d-image.png

          How can I overcome these limitations? Could you provide an example of using delegate to handle this? Thank you :D

          J 1 Reply Last reply 24 Jun 2023, 07:43
          0
          • H hieuht
            24 Jun 2023, 02:44

            Hi @JonB,

            Thank you for your reply. I'm using QTreeView and I can set the number of column by columCount method. But it is quite limited.

            First, the child's count cannot more than the parent's count.
            Second, the child's column size depend on the parent's size like the following picture.

            a45f1087-02a6-4a89-942e-26f8c9e6371d-image.png

            How can I overcome these limitations? Could you provide an example of using delegate to handle this? Thank you :D

            J Offline
            J Offline
            JonB
            wrote on 24 Jun 2023, 07:43 last edited by
            #5

            @hieuht said in Tree View with varying column counts:

            First, the child's count cannot more than the parent's count.

            Hmm, I did not see that mentioned. Indeed https://doc.qt.io/qt-6/qabstractitemmodel.html#columnCount says:

            In most subclasses, the number of columns is independent of the parent.

            Maybe it's a limitation on QTreeView rather than on the model?

            Second, the child's column size depend on the parent's size like the following picture.

            It looks like the child is only really using the parent's existing columns, just (according to you) it can only use the same number or fewer. Understandable, but not what your picture shows.

            Could you provide an example of using delegate to handle this?

            Quite beyond me! This is not just a simple delegate, it's virtually the drawing of a complete new table.

            @SGaist sometimes picks out something which KDE has added in https://api.kde.org/frameworks/index.html. You might look through that, maybe there is something there for you?

            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