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. [QML] Access the value of a different column in TableView
Forum Updated to NodeBB v4.3 + New Features

[QML] Access the value of a different column in TableView

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 4.4k Views 2 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.
  • K Offline
    K Offline
    Kaxu
    wrote on last edited by p3c0
    #1

    I have the following code:

    TableView {
                    id: table
                    width:parent.width *0.95
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.top:_descriptionPoste.bottom
                    anchors.topMargin: 10
                    selectionMode: SelectionMode.SingleSelection
                    itemDelegate :editableDelegate
                    TableViewColumn{ role: "min"  ; title: "Nb. min" ;width:(tableauTours.width/5); delegate: minComponent}
                    TableViewColumn{ role: "max" ; title: "Nb. max" ;width:(tableauTours.width/5); delegate: maxComponent}
    }
    

    Inside the component i found that i could have access (in the component) to the current data ( styleData.value ) , the current row index ( styleData.row ) and the current column index ( styleData.column) .
    However, if i'm clicking on the minComponent for example, i can have access the the min value, but how can i have access to the max value of the current row ?

    Thanks for reading

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kaxu
      wrote on last edited by
      #2

      Oh and by the way the model for the TableView is added dynamically when a button is clicked

      p3c0P 1 Reply Last reply
      0
      • K Kaxu

        Oh and by the way the model for the TableView is added dynamically when a button is clicked

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

        Hi @Kaxu,
        Access it from model instead of delegate as you already have row.

        157

        K 1 Reply Last reply
        0
        • p3c0P p3c0

          Hi @Kaxu,
          Access it from model instead of delegate as you already have row.

          K Offline
          K Offline
          Kaxu
          wrote on last edited by Kaxu
          #4

          @p3c0

          When i'm trying :
          table.model.get(0).id

          I get the following output :

          TypeError: Property 'get' of object SqlQueryModel(0x2599230) is not a function

          This is because the model come from a SqlDatabase i got from a class i created called SqlQueryModel that inherit from QSqlQueryModel.

          What can I do ?

          p3c0P 1 Reply Last reply
          0
          • K Kaxu

            @p3c0

            When i'm trying :
            table.model.get(0).id

            I get the following output :

            TypeError: Property 'get' of object SqlQueryModel(0x2599230) is not a function

            This is because the model come from a SqlDatabase i got from a class i created called SqlQueryModel that inherit from QSqlQueryModel.

            What can I do ?

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

            @Kaxu
            Using get wont work here. Since you have your own implemented model you will need to provide your own method in it which will return data at that row. Make this method Q_INVOKABLE to be able to access it from QML.

            157

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kaxu
              wrote on last edited by Kaxu
              #6

              Ok, here is the code i implemented :

              In SqlQueryModel.h
              Q_INVOKABLE QVariant data(const QModelIndex &index, int role) const;

              In SqlQueryModel.cpp

              QVariant SqlQueryModel::data(const QModelIndex &index, int role) const
              {
              QVariant value = QSqlQueryModel::data(index, role);
              if(role < Qt::UserRole)
              {
              value = QSqlQueryModel::data(index, role);
              }
              else
              {
              int columnIdx = role - Qt::UserRole - 1;
              QModelIndex modelIndex = this->index(index.row(), columnIdx);
              value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
              }
              qDebug() << "Error: " << lastError();
              return value;
              }

              And here is my qml code :
              table.model.data(0,0)

              but it keep returning "undefined", do you know why ?

              p3c0P 1 Reply Last reply
              0
              • K Kaxu

                Ok, here is the code i implemented :

                In SqlQueryModel.h
                Q_INVOKABLE QVariant data(const QModelIndex &index, int role) const;

                In SqlQueryModel.cpp

                QVariant SqlQueryModel::data(const QModelIndex &index, int role) const
                {
                QVariant value = QSqlQueryModel::data(index, role);
                if(role < Qt::UserRole)
                {
                value = QSqlQueryModel::data(index, role);
                }
                else
                {
                int columnIdx = role - Qt::UserRole - 1;
                QModelIndex modelIndex = this->index(index.row(), columnIdx);
                value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
                }
                qDebug() << "Error: " << lastError();
                return value;
                }

                And here is my qml code :
                table.model.data(0,0)

                but it keep returning "undefined", do you know why ?

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

                @Kaxu data is already a method in QSqlQueryModel. Moreover QML won't understand QModelIndex too.
                Instead create a separate Q_INVOKABLE method. Since you are using QSqlQueryModel you can use record to get row at particular index and then extract the data
                For eg:

                //in header file
                Q_INVOKABLE QString getData(int row);
                
                //in source
                QString getData(int row)
                {
                    return record(row).value(0).toString(); //will fetch row at index row and get data of particular database column
                }
                

                Then call this from QML using model property

                table.model.getData(3) //get data at row 3
                

                157

                1 Reply Last reply
                1
                • K Offline
                  K Offline
                  Kaxu
                  wrote on last edited by
                  #8

                  Wow this actually works ...
                  Thanks a lot p3c0 you're the best !!!

                  Do you work for Qt or something ?

                  p3c0P 1 Reply Last reply
                  0
                  • K Kaxu

                    Wow this actually works ...
                    Thanks a lot p3c0 you're the best !!!

                    Do you work for Qt or something ?

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

                    @Kaxu Thanks :).
                    No, Just a normal Qt addicted user :)

                    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