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. Dynamic Columns in TableView
Forum Updated to NodeBB v4.3 + New Features

Dynamic Columns in TableView

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 1 Posters 1.9k 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    I am using a TableView and giving it a list model from C++. However, I am finding that TableView is looking for roles that would correspond to each column (I think, not sure). I cannot get the TableView to display data from my model for some reason. But if I feed a QML ListModel to the same TableView I can get it to display. The QML ListModel seems to use roles very explicitly.

    So, I started wondering about dynamic views that might need a dynamic set of columns. Is there a way to do this with a QML TableView using a C++ model for the data? I would like to be able to query the number of columns returned from a database query and display the results of all columns without defining explicit roles, if possible.

    It may be that I am just not seeing the forest for the trees right now.

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      So, I looked at the source for QAbstractListModel and QAbstractItemModel and it looks like roleNames is a virtual function that is never defined. That means there are no default roles at all? I guess the default roles are only in QML types like ListView? This is really interesting and explains why I have been struggling with this.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        I "think" I found something that should work:
        https://stackoverflow.com/questions/27230818/qml-tableview-with-dynamic-number-of-columns

        It seems to show all the parts needed for dynamic roles.

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        1
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #4

          Can role name strings used in the interface to C++ be strings of numbers? example: "256"
          I have found that the code I am working on in using numbers to represent the roles. So that the roleNames function returns a list of number/string pairs that look like this:

          256,"256"
          257,"257"
          258,"258"
          etc
          

          However, the TableView only seems to be requesting role number 256 when calling the data() function on our QAbstractListModel. I cannot find the page that talks about roleNames in the model view programming docs pages for Qt.

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          0
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #5

            Painful...
            I changed the rolenames to this:

            1,"rn256"
            2,"rn257" 
            etc
            

            Now everything seems to work.

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            1

            • Login

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