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

SourceChanged signal in XmlListModel



  • In my code :

    XmlListModel {
    id: feedModel

        source: "http://" + rootmaster.currentFeed
        query: "/rss/channel/item"
        XmlRole { name: "title"; query: "title/string()" }
        // Remove any links from the description
        XmlRole { name: "description"; query: "fn:replace(description/string(), '\<a href=.*\/a\>', '')" }
        onSourceChanged: {
    
         console.log("count change: ",count);
        }
    
    }
    

    When I click first time on any item my result is : qml: count change: 0. It's bad
    But when I click any item after my result is : **qml: count change: 6 **. It's good
    Can everybody explain for me ?
    Thank you for your help !!!!


  • Moderators

    Hi @mrbongdem
    Which item are you clicking ? Perhaps you should use onCountChanged instead ?



  • Hi @p3c0 ,
    I tried onCountChanged and result is the same.
    I don't know why ?
    Thank for your replay.


  • Moderators

    @mrbongdem Can you post a complete minimal example to test ?



  • main.qml :

    import QtQuick 2.4
    import QtQuick.Controls 1.3
    import QtQuick.Window 2.2
    import QtQuick.Dialogs 1.2
    
    ApplicationWindow {
        title: qsTr("Hello World")
        width: 640
        height: 480
        visible: true
        Main_Gui{
    
        }
    
    
    }
    

    Main_Gui.qml :

    import QtQuick 2.0
    import QtQuick.Window 2.1
    import QtQuick.XmlListModel 2.0
    import QtQuick.Layouts 1.0
    import QtQuick.Window 2.1
    import QtQuick.Controls 1.4
    import QtQuick.Controls.Styles 1.4
    import QtQuick.Dialogs 1.0
    import QtQuick.Extras 1.4
    Rectangle {
        id:rootmaster
        anchors.fill: parent
        color: "#161616"
        property string currentFeed
        RssFeeds { id: rssFeeds }
        XmlListModel {
            id: feedModel
            source: "http://" + rootmaster.currentFeed
            query: "/rss/channel/item"
            XmlRole { name: "title"; query: "title/string()" }
            // Remove any links from the description
            XmlRole { name: "description"; query: "fn:replace(description/string(), '\<a href=.*\/a\>', '')" }
            onSourceChanged: {
             console.log("count change: ",count);
             console.log("source change: ",source);
            }
    
        }
    
        StackView {
            id: stackView
            anchors.fill: rootmaster
            x:200
            y:100
            initialItem: ListView {
                model: rssFeeds
                delegate: Button{
                    width: stackView.width
                    height: rootmaster.height * 0.125
                    text: title
                    onClicked: {
                        if (stackView.depth == 1) {
                            currentFeed = feed;
                            feedModel.reload();
                        }
                    }
                }
            }
    
        }
    }
    

    RssFeeds.qml :

    import QtQuick 2.2
    ListModel {
        ListElement { title: "xổ số Miền Nam"; feed: "xskt.com.vn/rss-feed/mien-nam-xsmn.rss";}
        ListElement { title: "xổ số Miền Trung"; feed: "xskt.com.vn/rss-feed/mien-trung-xsmt.rss";}
        ListElement { title: "xổ số Miền Bắc"; feed: "xskt.com.vn/rss-feed/mien-bac-xsmb.rss";}
        ListElement { title: "xổ số An Giang"; feed: "xskt.com.vn/rss-feed/an-giang-xsag.rss";}
        ListElement { title: "xổ số Bình Dương"; feed: "xskt.com.vn/rss-feed/binh-duong-xsbd.rss";}
        ListElement { title: "xổ số Bình Định"; feed: "xskt.com.vn/rss-feed/binh-dinh-xsbdi.rss";}
        ListElement { title: "xổ số Bạc Liêu"; feed: "xskt.com.vn/rss-feed/bac-lieu-xsbl.rss";}
        ListElement { title: "xổ số Bình Phước"; feed: "xskt.com.vn/rss-feed/binh-phuoc-xsbp.rss";}
        ListElement { title: "xổ số Bến Tre"; feed: "xskt.com.vn/rss-feed/ben-tre-xsbt.rss";}
    }
    

    Thank for your help !!


  • Moderators

    @mrbongdem

    onSourceChanged: { //<--- use onCountChanged here
             console.log("count change: ",count);
             console.log("source change: ",source);
            }
    


  • Thank you @p3c0
    I have tested it.


  • Moderators

    @mrbongdem It should work because the handler will be called only when the count has changed and when called it will always print the updated count.


Log in to reply