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. Different views require different model implementations - how to solve?
Forum Updated to NodeBB v4.3 + New Features

Different views require different model implementations - how to solve?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 547 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.
  • A Offline
    A Offline
    Asperamanca
    wrote on 22 Mar 2018, 15:18 last edited by
    #1

    I realized that QTableView and the QML TableView component need totally different data representations to work:

    • QTableView reads the data by row and column index, and uses the role to specify what particular aspect of data it wants
    • QML's TableView reads the data by row index, but only data with column index 0. Columns are implemented through different roles (which get names in QML, and can therefore be linked to columns)

    My idea was to have a single model implementation that can then be used both in a thin Qt Widget application (Desktop) and QML application (Embedded). But it looks as if I cannot write a model that satisfies both requirements.

    Any ideas how I could solve this?

    R 1 Reply Last reply 22 Mar 2018, 15:22
    0
    • A Asperamanca
      22 Mar 2018, 15:18

      I realized that QTableView and the QML TableView component need totally different data representations to work:

      • QTableView reads the data by row and column index, and uses the role to specify what particular aspect of data it wants
      • QML's TableView reads the data by row index, but only data with column index 0. Columns are implemented through different roles (which get names in QML, and can therefore be linked to columns)

      My idea was to have a single model implementation that can then be used both in a thin Qt Widget application (Desktop) and QML application (Embedded). But it looks as if I cannot write a model that satisfies both requirements.

      Any ideas how I could solve this?

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 22 Mar 2018, 15:22 last edited by raven-worx
      #2

      @Asperamanca
      Simply forward the role to the actual column index then:

      QVariant MyModel::data(const QModelIndex &index, int role)
      {
             
            if( role == <QML_ROLE> )
            {
                   QModelIndex proxyIndex = index.sibling( index.row(), <CORRESPONDING_COLUMN_FOR_QMLROLE> );
                   return this->data( proxyIndex, Qt::DisplayRole );
            }
      
           ....
           <<< Your "normal" implementation here >>>
      }
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      A 1 Reply Last reply 22 Mar 2018, 16:16
      5
      • R raven-worx
        22 Mar 2018, 15:22

        @Asperamanca
        Simply forward the role to the actual column index then:

        QVariant MyModel::data(const QModelIndex &index, int role)
        {
               
              if( role == <QML_ROLE> )
              {
                     QModelIndex proxyIndex = index.sibling( index.row(), <CORRESPONDING_COLUMN_FOR_QMLROLE> );
                     return this->data( proxyIndex, Qt::DisplayRole );
              }
        
             ....
             <<< Your "normal" implementation here >>>
        }
        
        A Offline
        A Offline
        Asperamanca
        wrote on 22 Mar 2018, 16:16 last edited by
        #3

        @raven-worx
        That's clever. Thanks!

        1 Reply Last reply
        0

        2/3

        22 Mar 2018, 15:22

        • Login

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