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?

Different views require different model implementations - how to solve?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 541 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.
  • A Offline
    A Offline
    Asperamanca
    wrote on 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?

    raven-worxR 1 Reply Last reply
    0
    • A Asperamanca

      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?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on 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
      5
      • raven-worxR raven-worx

        @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 last edited by
        #3

        @raven-worx
        That's clever. Thanks!

        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