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. Force QAbstractItemModel to update role names or call roleNames() again

Force QAbstractItemModel to update role names or call roleNames() again

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 2.9k 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.
  • M Offline
    M Offline
    MrBlueSky125
    wrote on last edited by
    #1

    I'm currently implementing a model, which basically exposes a SQL-Table to QML. You can access the fields through the row number and the column name (role name). Deriving from QSqlTableModel does the main work (while this model works with columns not role names).

    However I have a little trouble exposing the column names to qml. I reimplemented the roleNames() method, but it seems that this method is only called once in the liftime of the model (clearing, reseting the model does not cause the model to re-read the role names).
    So if the table name or the selected columns changes, either role names are missing or columns cannot be referenced.

    How can I trigger the model to call roleNames() again?

    raven-worxR 1 Reply Last reply
    0
    • M MrBlueSky125

      I'm currently implementing a model, which basically exposes a SQL-Table to QML. You can access the fields through the row number and the column name (role name). Deriving from QSqlTableModel does the main work (while this model works with columns not role names).

      However I have a little trouble exposing the column names to qml. I reimplemented the roleNames() method, but it seems that this method is only called once in the liftime of the model (clearing, reseting the model does not cause the model to re-read the role names).
      So if the table name or the selected columns changes, either role names are missing or columns cannot be referenced.

      How can I trigger the model to call roleNames() again?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @MrBlueSky125
      In Qt4 there was a setter. For which the docs say that it has to be called before the model is used.
      In Qt5 the setter was removed.

      So it seems is not intended that the role names change during runtime.
      The only thing i can think of is to call reset() and let the view forget everything it knows from the model and force it to regather all the data.
      The disadvantage of reset() is that all states (selection, etc.) are lost.

      But i am wondering why the roleNames can change in your model?

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

      M 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @MrBlueSky125
        In Qt4 there was a setter. For which the docs say that it has to be called before the model is used.
        In Qt5 the setter was removed.

        So it seems is not intended that the role names change during runtime.
        The only thing i can think of is to call reset() and let the view forget everything it knows from the model and force it to regather all the data.
        The disadvantage of reset() is that all states (selection, etc.) are lost.

        But i am wondering why the roleNames can change in your model?

        M Offline
        M Offline
        MrBlueSky125
        wrote on last edited by
        #3

        @raven-worx
        They can change because, theoratically the user can change the statement property of the model from 'SELECT column1, column2 FROM table' to 'SELECT * FROM table' (with possibly more columns and therefore roles).

        But I guess then the declared view does not fit to the model anymore (if there was no column3 before, why should it now displayed by the view?). So it makes sense to prevent these fundamental changes you can make to a model. Its basically another model then.

        Thx for the answer anyway, seems I haven't thought it through enough :)

        raven-worxR 1 Reply Last reply
        0
        • M MrBlueSky125

          @raven-worx
          They can change because, theoratically the user can change the statement property of the model from 'SELECT column1, column2 FROM table' to 'SELECT * FROM table' (with possibly more columns and therefore roles).

          But I guess then the declared view does not fit to the model anymore (if there was no column3 before, why should it now displayed by the view?). So it makes sense to prevent these fundamental changes you can make to a model. Its basically another model then.

          Thx for the answer anyway, seems I haven't thought it through enough :)

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @MrBlueSky125
          But the case you just described is a change of the column count and not a change of the rolenames?
          And column titles should be provided by headerData()

          Roles should normally be used to gather different information for a given index.
          And the roleNames() just exposes the roles as referenceable strings to QML.

          Related documentation.

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

          M 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @MrBlueSky125
            But the case you just described is a change of the column count and not a change of the rolenames?
            And column titles should be provided by headerData()

            Roles should normally be used to gather different information for a given index.
            And the roleNames() just exposes the roles as referenceable strings to QML.

            Related documentation.

            M Offline
            M Offline
            MrBlueSky125
            wrote on last edited by MrBlueSky125
            #5

            @raven-worx
            Thats because no QML view knows of/uses columns of models (even the QML TableView uses roles not columns). The QML views are orthogonal to the C++ Views. No headerData() either.
            So what I do is mapping the column to a role, so I can access fields in a delegate at all, like this:

            delegate: Label {
                text: "Column 3: " + column3
            }
            

            And for convenience, the columns should be referenced by the column name.

            raven-worxR 1 Reply Last reply
            0
            • M MrBlueSky125

              @raven-worx
              Thats because no QML view knows of/uses columns of models (even the QML TableView uses roles not columns). The QML views are orthogonal to the C++ Views. No headerData() either.
              So what I do is mapping the column to a role, so I can access fields in a delegate at all, like this:

              delegate: Label {
                  text: "Column 3: " + column3
              }
              

              And for convenience, the columns should be referenced by the column name.

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #6

              @MrBlueSky125
              i see.
              Would have never expected such a "misdesign" IMHO in QML

              But have you tried the reset approach yet?

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

              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