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 access data from a delegate's component.
QtWS25 Last Chance

how to access data from a delegate's component.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qt5.15.2qmltableviewtableviewcolumndelegate
4 Posts 2 Posters 1.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.
  • V Offline
    V Offline
    vinaygopal
    wrote on last edited by
    #1

    I have a TabeView object underwhich i have implemented various tableViewcolumns

    This is the code

    TableView {
            anchors.fill: parent
            clip: true
    
            model: peopleModel
    
            TableViewColumn {
                id: firstColumn
                title: "First Name"
                role: "one";
                width: 70;
                delegate: firstColumnComponent
            }
    
            Component{
                id:firstColumnComponent
                Item {
                    id: toplevelItem
                    property bool textpresent: styleData.value? true : false
                    TextEdit{
                        text: styleData.value
                    }
                }
            }
    
            TableViewColumn {title: "Last Name"; role: "two"; width: 70; delegate: secondColoumnComponent}
    
            Component{
                id:secondColoumnComponent
                Item {
                    id: secondColoumnparent
                    TextEdit{
                        text: firstColumn.delegate.textpresent  ? styleData.value : "Null" 
                    }
                }
            }
    

    here in the second column i am trying to access the property 'textpresent; which is in the firstColumn's Delegate.

    I tried various combinations in the second columns delegate i am not able to acess the property present inside the componenet.

    Is there anyway i can access the property present in the first column's delegate?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #4

      The model should provide this information for you. Create a role in the model which will return the correct value for any given cell. On QML side, just access that role and display it directly. No need for any get() method (which, by the way, is called data() https://doc.qt.io/qt-5/qabstractitemmodel.html#data, not get()).

      (Z(:^

      1 Reply Last reply
      2
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #2

        The model should hold the data you need, and you should only be getting it through the model.

        With some hacking you may be able to get this info from a delegate, but I strongly advise against doing that. Reason: delegates are controlled by the view. They are created an destroyed on the fly, without your control or knowledge. You may successfully get a value from a delegate now, only for the app to crash at another time. In all of Qt's item views, you should always take the data from the model. If this data is missing from the model - add it there.

        (Z(:^

        V 1 Reply Last reply
        0
        • sierdzioS sierdzio

          The model should hold the data you need, and you should only be getting it through the model.

          With some hacking you may be able to get this info from a delegate, but I strongly advise against doing that. Reason: delegates are controlled by the view. They are created an destroyed on the fly, without your control or knowledge. You may successfully get a value from a delegate now, only for the app to crash at another time. In all of Qt's item views, you should always take the data from the model. If this data is missing from the model - add it there.

          V Offline
          V Offline
          vinaygopal
          wrote on last edited by
          #3

          @sierdzio In order for me to get the Data from the model, i will have to have the row Information right? and when i try to do something like
          peopleModel.get(Row) i get an error like this


          TypeError: Property 'get' of object QAbstractTableModel(0x77fd10) is not a function

          Does this mean i have to implement my own 'get' function in the model?

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #4

            The model should provide this information for you. Create a role in the model which will return the correct value for any given cell. On QML side, just access that role and display it directly. No need for any get() method (which, by the way, is called data() https://doc.qt.io/qt-5/qabstractitemmodel.html#data, not get()).

            (Z(:^

            1 Reply Last reply
            2

            • Login

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