How to use listview onAdd signal ?



  • I assign a model to a listview ,then the listview will add some items according to the delegate .But I can't connect the listview onAdd signal .
    The code below :
    @import Qt 4.7

    ListView
    {
    id: listView
    width:200
    height:800

    ListModel
    {
    id:listmodel
    ListElement
    {
    text:'text1'
    }
    ListElement
    {
    text:'text2'
    }
    }

    delegate:Rectangle
    {
    width:parent.width
    height:64

    Text
    {
    text:model.text
    }
    }

    onAdd:
    {
    console.log('on add sig')
    }
    model:listmodel
    }@

    use qmlviewer to run it ,but it comes a error . I want two 'on add sig ' output ,but nothing .So how to do ?


  • Moderators

    Hi,

    You need to move the signal onAdd into the delegate. Also as per "this":https://bugreports.qt-project.org/browse/QTBUG-29384 bug report i modified your code as follows and the signal gets fired
    @
    import QtQuick 2.0

    ListView
    {
    id: listView
    width:200
    height:800

    ListModel
    {
        id:listmodel
        ListElement{
            text:'text1'
        }
        ListElement{
            text:'text2'
        }
    }
    
    delegate: Rectangle {
        Text { text:model.text }
    
        ListView.onAdd: {
            console.log("ad")
        }
    }
    
    model:listmodel
    
    MouseArea {
        anchors.fill: parent
        onClicked: {
                model.append({text: "Click"});
        }
    }
    

    }

    @



  • thanks but still no message output
    [quote author="p3c0" date="1382354266"]Hi,

    You need to move the signal onAdd into the delegate. Also as per "this":https://bugreports.qt-project.org/browse/QTBUG-29384 bug report i modified your code as follows and the signal gets fired
    @
    import QtQuick 2.0

    ListView
    {
    id: listView
    width:200
    height:800

    ListModel
    {
        id:listmodel
        ListElement{
            text:'text1'
        }
        ListElement{
            text:'text2'
        }
    }
    
    delegate: Rectangle {
        Text { text:model.text }
    
        ListView.onAdd: {
            console.log("ad")
        }
    }
    
    model:listmodel
    
    MouseArea {
        anchors.fill: parent
        onClicked: {
                model.append({text: "Click"});
        }
    }
    

    }

    @[/quote]

    [quote author="p3c0" date="1382354266"]Hi,

    You need to move the signal onAdd into the delegate. Also as per "this":https://bugreports.qt-project.org/browse/QTBUG-29384 bug report i modified your code as follows and the signal gets fired
    @
    import QtQuick 2.0

    ListView
    {
    id: listView
    width:200
    height:800

    ListModel
    {
        id:listmodel
        ListElement{
            text:'text1'
        }
        ListElement{
            text:'text2'
        }
    }
    
    delegate: Rectangle {
        Text { text:model.text }
    
        ListView.onAdd: {
            console.log("ad")
        }
    }
    
    model:listmodel
    
    MouseArea {
        anchors.fill: parent
        onClicked: {
                model.append({text: "Click"});
        }
    }
    

    }

    @[/quote]


  • Moderators

    Hi,

    It works for me. Did you get any errors ?
    Did you click on the list ? As i have added that message when one click's on the List.
    @
    MouseArea {
    anchors.fill: parent
    onClicked: {
    model.append({text: "Click"});
    }
    }
    @

    And message is here in
    @
    ListView.onAdd: {
    console.log("ad")
    }
    @



  • I think the message 'ad' should output even if I don't click ,because the connected signal is onAdd but not onClick ,is that right?
    [quote author="p3c0" date="1382356560"]Hi,

    It works for me. Did you get any errors ?
    Did you click on the list ? As i have added that message when one click's on the List.
    @
    MouseArea {
    anchors.fill: parent
    onClicked: {
    model.append({text: "Click"});
    }
    }
    @

    And message is here in
    @
    ListView.onAdd: {
    console.log("ad")
    }
    @[/quote]



  • Try to add x property to delegate so you can see better:

    @delegate: Rectangle {
    x: model.index * 50

        Text { text:model.text }
    
        ListView.onAdd: {
            console.log("ad")
        }
    }@


  • still not 'ad' output
    [quote author="Ever" date="1382423093"]Try to add x property to delegate so you can see better:

    @delegate: Rectangle {
    x: model.index * 50

        Text { text:model.text }
    
        ListView.onAdd: {
            console.log("ad")
        }
    }@[/quote]
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.