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. how to get the current index on click of an image for table view

how to get the current index on click of an image for table view

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlqt5tableviewcolumntable view
2 Posts 2 Posters 920 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.
  • V Offline
    V Offline
    vinaygopal
    wrote on 15 Sept 2021, 10:33 last edited by
    #1

    i have a requirement where i have to implement fav/unfav on table view

    i have created a model on the c++ side and got the UI on the QML side

    the QML code is

    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Table View Example")
    
        TableView {
            id:peopleTable
            anchors.fill: parent
    
            signal editfinished(string new_string, string original_string)
            onEditfinished: {
                console.log("this signal called ",new_string, " and ",original_string)
                peopleModel.writeData(new_string , original_string)
            }
    
            clip: true
            model: peopleModel
    
            TableViewColumn {
                id: firstColumn
                title: "First_Name"
                property var something: delegate.item
                role: "one";
                width: 200;
                delegate: firstColumnComponent
            }
    
            Component{
                id:firstColumnComponent
                Item {
                    id: toplevelItem
                    property bool textpresent: true
                    Row{
                        spacing: 10
                        TextField{
                            id:te
                            width: 100
                            text: styleData.value
                            maximumLength: 20
                            onAccepted:{
                                console.log("the editing is finished to",te.text)
                                peopleTable.editfinished(te.text,styleData.value)
                            }
                        }
                        Image {
                            id: fav_image
                            source: "qrc:/favourite_icon.png"
                            height: 20
                            width: 40
                            MouseArea{
                                anchors.fill: parent
                                onClicked: {
                                    console.log("the current index is",peopleTable.currentRow)
                                }
                            }
                        }
                    }
                }
            }
    

    What happens is when i click the favourite image i get the currentRow as -1 thats because my row is not selected, is There a way to select the row on click of the delegate image, or obtain the corresponding row on click.

    My UI looks like this.

    d72e4c46-5a24-42fc-8d3c-0430016430eb-image.png

    J 1 Reply Last reply 15 Sept 2021, 12:02
    0
    • V vinaygopal
      15 Sept 2021, 10:33

      i have a requirement where i have to implement fav/unfav on table view

      i have created a model on the c++ side and got the UI on the QML side

      the QML code is

      Window {
          visible: true
          width: 640
          height: 480
          title: qsTr("Table View Example")
      
          TableView {
              id:peopleTable
              anchors.fill: parent
      
              signal editfinished(string new_string, string original_string)
              onEditfinished: {
                  console.log("this signal called ",new_string, " and ",original_string)
                  peopleModel.writeData(new_string , original_string)
              }
      
              clip: true
              model: peopleModel
      
              TableViewColumn {
                  id: firstColumn
                  title: "First_Name"
                  property var something: delegate.item
                  role: "one";
                  width: 200;
                  delegate: firstColumnComponent
              }
      
              Component{
                  id:firstColumnComponent
                  Item {
                      id: toplevelItem
                      property bool textpresent: true
                      Row{
                          spacing: 10
                          TextField{
                              id:te
                              width: 100
                              text: styleData.value
                              maximumLength: 20
                              onAccepted:{
                                  console.log("the editing is finished to",te.text)
                                  peopleTable.editfinished(te.text,styleData.value)
                              }
                          }
                          Image {
                              id: fav_image
                              source: "qrc:/favourite_icon.png"
                              height: 20
                              width: 40
                              MouseArea{
                                  anchors.fill: parent
                                  onClicked: {
                                      console.log("the current index is",peopleTable.currentRow)
                                  }
                              }
                          }
                      }
                  }
              }
      

      What happens is when i click the favourite image i get the currentRow as -1 thats because my row is not selected, is There a way to select the row on click of the delegate image, or obtain the corresponding row on click.

      My UI looks like this.

      d72e4c46-5a24-42fc-8d3c-0430016430eb-image.png

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 15 Sept 2021, 12:02 last edited by
      #2

      @vinaygopal
      according to the documentation:

      https://doc.qt.io/qt-5/qml-qtquick-controls-tableviewcolumn.html#delegate-prop

      this should do the trick:

      Component{
                  id:firstColumnComponent
                  Item {
                      id: toplevelItem
                      property bool textpresent: true
                      Row{
                          spacing: 10
                          TextField{
                              id:te
                              width: 100
                              text: styleData.value
                              maximumLength: 20
                              onAccepted:{
                                  console.log("the editing is finished to",te.text)
                                  peopleTable.editfinished(te.text,styleData.value)
                              }
                          }
                          Image {
                              id: fav_image
                              source: "qrc:/favourite_icon.png"
                              height: 20
                              width: 40
                              MouseArea{
                                  anchors.fill: parent
                                  onClicked: {
                                      console.log("the current index is",styleData.row) //Change happened here
                                  }
                              }
                          }
                      }
                  }
              }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1

      1/2

      15 Sept 2021, 10:33

      • Login

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