Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Access role enum value from QML

Access role enum value from QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 4 Posters 1.4k Views 3 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.
  • E Offline
    E Offline
    egor.utsov
    wrote on last edited by
    #1

    Hi. I would like to know if it is possible to access current role enum value from a delegate. I would like to use this value further to check if value was changed or not by direct call to model. For sure I can just pass role name directly to checking method, but prefer some safer way to use role.

    JKSHJ 2 Replies Last reply
    0
    • E egor.utsov

      Hi. I would like to know if it is possible to access current role enum value from a delegate. I would like to use this value further to check if value was changed or not by direct call to model. For sure I can just pass role name directly to checking method, but prefer some safer way to use role.

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @egor.utsov said in Access role enum value from QML:

      I would like to know if it is possible to access current role enum value from a delegate.

      Yes, it is. Reimplement QAbstractItemModel::roleNames() in C++ and add required property <roleDataType> <roleName> to your delegate in QML: https://doc.qt.io/qt-6/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel-subclass

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by
        #3

        I don't understand the question. What is current role enum?

        Is this related to the model-view framework? If so, the model reports changes via various signals such as QAbstractItemModel::dataChanged. Just reference the data in a binding. There's no need to have a separate function call.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        0
        • E egor.utsov

          Hi. I would like to know if it is possible to access current role enum value from a delegate. I would like to use this value further to check if value was changed or not by direct call to model. For sure I can just pass role name directly to checking method, but prefer some safer way to use role.

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @egor.utsov said in Access role enum value from QML:

          check if value was changed or not by direct call to model.

          Actually... what do you mean by "changed... by direct call to model"?

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • E Offline
            E Offline
            egor.utsov
            wrote on last edited by egor.utsov
            #5

            @jeremy_k @JKSH I've implement method isDirty in the model to check if the value in the cell was changed, but not submitted. And now I want to pass cell index into this method. But index itself is not enough, because in qml role used instead of column number. And I am asking - how to obtain role integer (not role name) that is according to role name used by delegate to access model data?
            Or maybe there is the better way of how to do it.

            jeremy_kJ 1 Reply Last reply
            0
            • E egor.utsov

              @jeremy_k @JKSH I've implement method isDirty in the model to check if the value in the cell was changed, but not submitted. And now I want to pass cell index into this method. But index itself is not enough, because in qml role used instead of column number. And I am asking - how to obtain role integer (not role name) that is according to role name used by delegate to access model data?
              Or maybe there is the better way of how to do it.

              jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on last edited by jeremy_k
              #6

              @egor.utsov said in Access role enum value from QML:

              Or maybe there is the better way of how to do it.

              That's what I'm thinking. Have you considered storing the uncommitted data in another role in the model? Either add a function to synchronize the two, or assign to the permanent role and let the view invoke setData().

              Models used in QML also have columns. Heavy use of one-dimensional views and the ease of defining delegates might have led to fewer multicolumn examples.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              E 1 Reply Last reply
              0
              • jeremy_kJ jeremy_k

                @egor.utsov said in Access role enum value from QML:

                Or maybe there is the better way of how to do it.

                That's what I'm thinking. Have you considered storing the uncommitted data in another role in the model? Either add a function to synchronize the two, or assign to the permanent role and let the view invoke setData().

                Models used in QML also have columns. Heavy use of one-dimensional views and the ease of defining delegates might have led to fewer multicolumn examples.

                E Offline
                E Offline
                egor.utsov
                wrote on last edited by
                #7

                @jeremy_k the way with separate role for uncommited data seems more complex for me. Columns is not used in QML as far as I understand. We use roles instead. Like model.someField to access cell value in a row. Using separate role for uncommited data simply means doubling of roles (two for each cell - commited data and uncommited) and at the same time you need to decide, which data to show at the moment.

                JKSHJ 1 Reply Last reply
                0
                • E egor.utsov

                  @jeremy_k the way with separate role for uncommited data seems more complex for me. Columns is not used in QML as far as I understand. We use roles instead. Like model.someField to access cell value in a row. Using separate role for uncommited data simply means doubling of roles (two for each cell - commited data and uncommited) and at the same time you need to decide, which data to show at the moment.

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  @egor.utsov said in Access role enum value from QML:

                  Using separate role for uncommited data simply means doubling of roles (two for each cell - commited data and uncommited) and at the same time you need to decide, which data to show at the moment.

                  Why is doubling the number of roles a problem? You could go even further and call isDirty() from data() only (for another role), so that you don't need to call it from QML:

                  delegate: Label {
                      required property string committedData
                      required property string uncommittedData
                      required property bool isDirty
                  
                      text: isDirty ? uncommittedData : committedData
                  }
                  

                  in qml role used instead of column number.

                  Not necessarily. You can absolutely use QAbstractTableModel with TableView: https://doc.qt.io/qt-6/qml-qtquick-tableview.html#c-models

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  E 1 Reply Last reply
                  0
                  • JKSHJ JKSH

                    @egor.utsov said in Access role enum value from QML:

                    Using separate role for uncommited data simply means doubling of roles (two for each cell - commited data and uncommited) and at the same time you need to decide, which data to show at the moment.

                    Why is doubling the number of roles a problem? You could go even further and call isDirty() from data() only (for another role), so that you don't need to call it from QML:

                    delegate: Label {
                        required property string committedData
                        required property string uncommittedData
                        required property bool isDirty
                    
                        text: isDirty ? uncommittedData : committedData
                    }
                    

                    in qml role used instead of column number.

                    Not necessarily. You can absolutely use QAbstractTableModel with TableView: https://doc.qt.io/qt-6/qml-qtquick-tableview.html#c-models

                    E Offline
                    E Offline
                    egor.utsov
                    wrote on last edited by
                    #9

                    @JKSH the issue here is that I use custom view for my data and my model have tree-like structure. I use DelegateModel to display different levels of it with custom delegates and the delegate is applied to the whole row of the table.

                    1 Reply Last reply
                    0
                    • GrecKoG Offline
                      GrecKoG Offline
                      GrecKo
                      Qt Champions 2018
                      wrote on last edited by
                      #10

                      Another way to do it if you have custom delegates anyway is to return a gadget struct for each role with a value property and an isDirty property.

                      E 1 Reply Last reply
                      1
                      • GrecKoG GrecKo

                        Another way to do it if you have custom delegates anyway is to return a gadget struct for each role with a value property and an isDirty property.

                        E Offline
                        E Offline
                        egor.utsov
                        wrote on last edited by
                        #11

                        @GrecKo that's an interesting approach. Thank you for the idea! This thread could be considered as solved.

                        1 Reply Last reply
                        0
                        • E egor.utsov has marked this topic as solved on

                        • Login

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