Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved Different views require different model implementations - how to solve?

    General and Desktop
    2
    3
    402
    Loading More Posts
    • 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
      Asperamanca last edited by

      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-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @Asperamanca last edited by 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 >>>
        }
        

        --- 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 Reply Quote 5
        • A
          Asperamanca @raven-worx last edited by

          @raven-worx
          That's clever. Thanks!

          1 Reply Last reply Reply Quote 0
          • First post
            Last post