Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML-How-to? Anonymous Access To TableView From Within Delegates



  • Hi,

    Using QML, I'm writing a custom itemDelegate and rowDelegate that I want to be able to
    use across any number of different TableView instances.

    As the code to follows shows, I've been able to accomplish this--but only in a way that I'm
    extremely dubious about it working in future (or previous, for that matter) releases of Qt.
    I'm currently using Qt 5.2.

    @
    Component {
    id: tableRowDelegate

    Rectangle {
    height: 16
    color: styleData.selected ? "#448" : (styleData.alternate? "#eee" : "#fff")

         property TableView tableView
    
        Component.onCompleted: {
              if (styleData.row > -1)  // Not entirely sure why this is needed -- worse, I don't know why it works; it just does
                    tableView = parent.parent.parent.parent.parent.parent.parent.parent
        }
    
     // Other delegate code omitted for brevity
    

    }
    }

    Component {

    id: tableCellDelegate
    
    Rectangle {
        width: parent.width
        height: parent.height
        color: "transparent"
        border.color: "#a3b3b3"
        border.width: 1
        radius: 5
    
        property TableView tableView
    
        Component.onCompleted: {
            tableView = parent.parent.parent.parent.parent.parent.parent.parent.parent
        }
    
     // Other delegate code omitted for brevity
    }
    

    }
    @

    Once these delegates are constructed, access to the enclosing TableView is trivial
    (and anonymous) by prefixing the property name with 'tableView.'; i.e.
    @
    tableView.alternatingRowColors
    @

    just for example.

    Is there really no better way to access the view from within a delegate without
    hard-coding the ancestry, or worse: without hard-binding explicitly to the id of
    the TableView?

    Thanks for any thoughts!

    wahynes



  • Your observation is correct and I think this is a valid issue.

    In a ListView delegates can access the "ListView" attached property to get access to the View. For technical reasons I don't think we can attach a TableView property in exactly the same manner but there should still be a similar attached property available to delegates.

    Perhaps you could file a suggestion with a title such as:
    "Provide a way way to access the view from inside a TableView delegate". So that we can track this.

    As far as workarounds goes, you are correct that the parent hierarchy is likely to change in future versions. In fact I believe it will change already in 5.3 due to performance optimizations. A slightly more reliable way would be to create a simple loop that iterates up the parent hierarchy until it hits an object that has Accessible.role === Accessible.Table or similar.



  • Thanks, Jens,

    I posted this same question on StackOverflow. A couple of suggestions
    from one commenter, (plus about 10 hours of banging my head against
    my keyboard) and I actually came up with a fairly painless way to achieve
    this that avoids using the id AND avoids making any assumptions
    about the internal ancestry of a delegate.

    Rather than duplicating the solution here, you can read about it there:
    "Anonymous Access To TableView.":http://stackoverflow.com/questions/23522186/qml-how-to-anonymously-access-to-tableview-from-within-delegates:

    It's not quite as straightforward as the exposure of the model to the
    delegate, but it's pretty close.

    wahynes


Log in to reply