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. When to use columns versus custom roles in models?
Forum Updated to NodeBB v4.3 + New Features

When to use columns versus custom roles in models?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 852 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.
  • P Offline
    P Offline
    patrickkidd
    wrote on last edited by
    #1

    If I subclass QAbstractListModel, how does one decide when to use columns versus rows for list item attributes? I am only using custom views in Qml, using rowNames to register item attributes that can be accessed. In fact, this seems like the only way to access item attributes in Qml since there isn't a clean way to access a row item's column value, and even then you'd have to map them to names.

    But whenever overriding Model.data() or Model.setData() I am always querying by role and wondering why I am not using columns.

    Just trying to get the basic MVC concepts straight here.

    https://alaskafamilysystems.com/

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

      Hi,

      In the context of QML, the roles are used to create a mapping between the names you'll use in QML and the columns you want to access in your C++ models.

      See here

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

      P 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        In the context of QML, the roles are used to create a mapping between the names you'll use in QML and the columns you want to access in your C++ models.

        See here

        P Offline
        P Offline
        patrickkidd
        wrote on last edited by
        #3

        @SGaist OK, so in QML roles and columns are synonymous in terms of data values? I sort of reflexively ended up doing things like this to allow setting of values from Python/C++ as well as QML:

            def setData(self, index, value, role=Qt.EditRole):
                if self.isColumn(index, self.DATETIME) or role == self.DateTimeRole:
        

        That always seemed pretty hack-ish.

        And I assume the above only applies for list models, since table models different use of columns? If so then it seems that there is a problem with the concept of column and role parity.

        https://alaskafamilysystems.com/

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

          No they are not synonymous.

          If you go through the example, you'll see that the names are associated with UserRoles.

          These user roles are what you will use to retrieve the right value from your data structure.

          If you take the SQL model example from the link I gave in my previous post, you'll see that they provide a mapping so that you can access the database data from the QML side based on the table column names.

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

          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