Different views require different model implementations - how to solve?



  • 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?


  • Moderators

    @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 >>>
    }
    


  • @raven-worx
    That's clever. Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.