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. [SOLVED]How to get the value of a row in TableView using QML?

[SOLVED]How to get the value of a row in TableView using QML?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.0k 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.
  • C Offline
    C Offline
    CoderJeff
    wrote on last edited by CoderJeff
    #1

    I know the property currentRow, but I want to get the value instead of the index.

    Is there any method?

    p3c0P 1 Reply Last reply
    0
    • C CoderJeff

      I know the property currentRow, but I want to get the value instead of the index.

      Is there any method?

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @CoderJeff Since you already have the index or row number get the value from model. Ofcourse the method depends on which model type you are using.

      157

      C 1 Reply Last reply
      0
      • p3c0P p3c0

        @CoderJeff Since you already have the index or row number get the value from model. Ofcourse the method depends on which model type you are using.

        C Offline
        C Offline
        CoderJeff
        wrote on last edited by
        #3

        @p3c0

        Yes, I tried to do it like what you said. But the model is from C++:

        TableView {
        id: tableView
        ......
        model: myModelCpp.model()
        ......
        }

        The type of myModelCpp is MySqlModel, which inherits QSqlTableModel.

        Do I need to add a get function in C++ code?

        p3c0P 1 Reply Last reply
        0
        • C CoderJeff

          @p3c0

          Yes, I tried to do it like what you said. But the model is from C++:

          TableView {
          id: tableView
          ......
          model: myModelCpp.model()
          ......
          }

          The type of myModelCpp is MySqlModel, which inherits QSqlTableModel.

          Do I need to add a get function in C++ code?

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by p3c0
          #4

          @CoderJeff Yes add a Q_INVOKABLE get (or any other suitable name) function.

          157

          C 1 Reply Last reply
          1
          • p3c0P p3c0

            @CoderJeff Yes add a Q_INVOKABLE get (or any other suitable name) function.

            C Offline
            C Offline
            CoderJeff
            wrote on last edited by CoderJeff
            #5

            @p3c0
            I feel it is another unreasonable design about QML.

            I think whatever and wherever the model is, once it is in QML, it should be converted to a kind of unified type like List or something else, and QML provides an unified interface that is convenient to call.

            In addition, I tried to add the same code in ComboBox:

            ComboBox {
            ......
            model: myModelCpp.model()
            ......
            }

            But it did not work, showing that the model is undefined.

            Actually, either TableView or ComboBox, for us, their model are both a kind of data set, which should be used everywhere. Otherwise, we need to think about too much.

            p3c0P 1 Reply Last reply
            0
            • C CoderJeff

              @p3c0
              I feel it is another unreasonable design about QML.

              I think whatever and wherever the model is, once it is in QML, it should be converted to a kind of unified type like List or something else, and QML provides an unified interface that is convenient to call.

              In addition, I tried to add the same code in ComboBox:

              ComboBox {
              ......
              model: myModelCpp.model()
              ......
              }

              But it did not work, showing that the model is undefined.

              Actually, either TableView or ComboBox, for us, their model are both a kind of data set, which should be used everywhere. Otherwise, we need to think about too much.

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @CoderJeff I suspect you are definitely doing something wrong. Personally I have been using C++ models and never faced any major problems. Same C++ model in my case works perfectly with ListView and ComboBox. In case of ComboBox you have to use textRole to populate but it is not related to model undefined.
              Eg:

              ComboBox {
                  model: myModel
                  textRole: 'title'
              }
              

              157

              C 1 Reply Last reply
              0
              • p3c0P p3c0

                @CoderJeff I suspect you are definitely doing something wrong. Personally I have been using C++ models and never faced any major problems. Same C++ model in my case works perfectly with ListView and ComboBox. In case of ComboBox you have to use textRole to populate but it is not related to model undefined.
                Eg:

                ComboBox {
                    model: myModel
                    textRole: 'title'
                }
                
                C Offline
                C Offline
                CoderJeff
                wrote on last edited by
                #7

                @p3c0
                You are right. My ComboBox code works according to your method.

                QML is like an unearth buried treasure. As a beginner, I have a very long way to explore it.

                Thank you p3c0.

                p3c0P 1 Reply Last reply
                0
                • C CoderJeff

                  @p3c0
                  You are right. My ComboBox code works according to your method.

                  QML is like an unearth buried treasure. As a beginner, I have a very long way to explore it.

                  Thank you p3c0.

                  p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  @CoderJeff You're Welcome :)
                  I would suggest you to start from QML basics to make the roots strong.

                  157

                  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