Results 1 to 2 of 2

Thread: No index in TableView's delegate - how to reference actual model data object?

  1. #1
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default No index in TableView's delegate - how to reference actual model data object?

    Hi,

    I need a clickable QML element that is model-based and gives back a reference to its model entry (a complex Object that lives in a QAbstractListModel) on click.

    When the Element is generated with a repeater I could use this code (works!):
    Qt Code:
    1. Repeater {
    2. id: buttonRepeater
    3. model: loaderModel
    4. delegate: CustomButton {
    5. id: customButtonDelegate
    6. objectName: model.name
    7. buttonColor: model.buttonColor
    8. onClicked: { loaderModel.buttonClicked(loaderModel.get(index)) }
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    However, within a TableViewColumn this does not work, the latter apparently lacks an "index" (doesn't work!):
    Qt Code:
    1. TableView {
    2. id: dynamicTableViewBasis
    3. model: sceneLoader.loaderModel
    4.  
    5. Component {
    6. id: columnComponent
    7. TableViewColumn {
    8. width: 70
    9. delegate: CustomButton {
    10. id: buttonDelegate
    11. objectName: model.nameTop
    12. buttonColor: model.buttonColor
    13. onClicked: {sceneLoader.loaderModel.buttonClicked(sceneLoader.loaderModel.get(index)) }
    To copy to clipboard, switch view to plain text mode 

    I recieve an error message that says
    ReferenceError: index is not defined
    How can I achieve that kind of call (flawlessly working inside a repeater) in a QML TableView?

    [Please note that the table's columns are being generated dynamically, thus the "Component" stuff.]

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: No index in TableView's delegate - how to reference actual model data object?

    While it is kind of annoying that model.index isn't just available as the model's row, you can use styleData.row instead.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    sedi (28th February 2016)

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 20:39
  2. Replies: 2
    Last Post: 27th October 2012, 10:09
  3. Replies: 1
    Last Post: 7th August 2012, 13:41
  4. Replies: 12
    Last Post: 24th July 2010, 09:12
  5. How to populate delegate with model data.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2008, 10:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.