Results 1 to 5 of 5

Thread: How to set an .xml file as datasource to Listview

  1. #1
    Join Date
    Jul 2016
    Posts
    53
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default How to set an .xml file as datasource to Listview

    Hi,

    I am writing an rexlog.xml will be the data source to Listview in main.qml. But, The listview is not showing the value. In the xml the values are written and it is accessable.

    Query: I suspect, there will be performance issue, reading from .xml file and loading to Listview, Instead of that Is it good to load the data initially to QList or any similiar, so the listview can easily fetch the data for many events like scroll up/down.

    Which option will be better? The xml entry can be atleast 80-100 set of data.

    Code snippets:


    main.qml
    -----------
    Qt Code:
    1. property string strXMLPath: System.userHomePath + "/rexlog.xml" //.xml file stored in the c:\user\proile\rexlog.xml
    2.  
    3.  
    4. XmlListModel {
    5. id: idXMLListModel
    6. source: strXMLPath
    7. query: "/catalog/book"
    8. XmlRole {name: "title"; query: "title/string()"}
    9.  
    10. XmlRole { name: "first_author"; query: "author[1]/string()" }
    11. XmlRole { name: "year"; query: "year/number()" }
    12. }
    13.  
    14.  
    15. //Note: Add the click event for ListView
    16. ListView {
    17. anchors {
    18. left: parent.left
    19. leftMargin: 10 * scaleFactor
    20. right: parent.right
    21. rightMargin: 10 * scaleFactor
    22. top: rectangleToolBar.bottom
    23. topMargin: 10 * scaleFactor
    24. bottom: rectangleStatusBar.top
    25. bottomMargin: 10 * scaleFactor
    26. }
    27.  
    28. model: idXMLListModel
    29. delegate: Column {
    30. Layout.alignment: Qt.AlignCenter
    31. Text { text: title + " (" + type + ")"; font.bold: wanted }
    32. Text { text: first_author }
    33. Text { text: year }
    34. }
    35. }
    36.  
    37. Component {
    38. id: comsearchDelegate
    39.  
    40. Row {
    41. spacing: 10 * scaleFactor
    42.  
    43. Image {
    44. id: imageItem
    45. width: 100 * scaleFactor
    46. height: 70 * scaleFactor
    47. source: thumbnailUrl
    48. }
    49.  
    50. Column {
    51. Layout.alignment: Qt.AlignTop
    52. Text { text: Title; font { pixelSize: 14 * scaleFactor; bold: true } }
    53.  
    54. }
    55. }
    56. }
    To copy to clipboard, switch view to plain text mode 

    .xml
    -----
    Qt Code:
    1. <?xml version="1.0" encoding="ISO-8859-1"?>
    2.  
    3. -<catalog>
    4.  
    5.  
    6. -<book wanted="true" type="Online">
    7.  
    8. <title>Qt 5 Cadaques</title>
    9.  
    10. <year>2014</year>
    11.  
    12. <author>Juergen Bocklage-Ryannel</author>
    13.  
    14. <author>Johan Thelin</author>
    15.  
    16. </book>
    17.  
    18.  
    19. -<book type="Hardcover">
    20.  
    21. <title>C++ GUI Programming with Qt 4</title>
    22.  
    23. <year>2006</year>
    24.  
    25. <author>Jasmin Blanchette</author>
    26.  
    27. <author>Mark Summerfield</author>
    28.  
    29. </book>
    30.  
    31.  
    32. -<book type="Paperback">
    33.  
    34. <title>Programming with Qt</title>
    35.  
    36. <year>2002</year>
    37.  
    38. <author>Matthias Kalle Dalheimer</author>
    39.  
    40. </book>
    41.  
    42. </catalog>
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 1st October 2016 at 15:08. Reason: missing [code] tags

  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: How to set an .xml file as datasource to Listview

    If you have XML data then the XmlListModel is quite efficient in displaying it.

    If you have the data in a different data structure and write it to XML just to have it displayed then creating a list or custom model would likely be better.

    Regarding the XML not loading, what you have debugged so far?
    What's the model's status?
    Also, what do you have these "-" in there for?

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to set an .xml file as datasource to Listview

    Also, what do you have these "-" in there for?
    If those are actually in the XML, then "-<book ...>" is not valid XML and the XMLListModel is probably rejecting it. The only place that extra text characters is allowed in valid XML is within elements; eg. between "<book>" and "</book>" tags. These "-" characters are between tags, and thus makes the XML invalid.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Jul 2016
    Posts
    53
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: How to set an .xml file as datasource to Listview

    Hi,

    I tried according to your instructions. The below xml is properly read by the listView. But at the same time, I stored in the C:\userprofile\reg.xml, Then the value is not readable.


    Working Code Snippet:

    XmlListModel {
    id: idXMLListModel
    xml: "<Download><Maps><Title>MINUSCAOffice</Title><Title>MINUSCAOffice2</Title></Maps></Download>" //This xml working properly.
    query: "/Download/Maps/Title"
    XmlRole {
    name: "Title"
    query: "string()"
    }
    }

    Not working xml: //The Dash and xml header are added by itself.
    <?xml version="1.0"?>
    -<Download>
    -<Maps>
    <Title>MINUSCAOffice</Title>
    <Title>MINUSCAOffice2</Title>
    </Maps>
    </Download>

    Query: I had created the reg.xml when the first time login authenticated and downloaded items details are stored in xml. So I can read the reg.xml values in Readxml.CPP and just want to pass it to ListView, B'cos binding the xmlListmodel to listview brings trouble like mentioned earlier. Is any way to pass the value from Readxml.cpp to main.qml listview like passing as arguments?

    Ex:
    ListView {
    id: idListView
    anchors.fill: parent

    model: objReadxml.model() //What will be the argument type?
    delegate: comsearchDelegate
    spacing: 10 * scaleFactor
    clip: true


    Thanks in advance.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to set an .xml file as datasource to Listview

    Not working xml: //The Dash and xml header are added by itself.
    What do you mean, "added by itself"? If you are writing the file, then it is your code that is putting in the "-" characters. If it is not your code, then the code you are using is writing invalid XML to the file.

    You will not be able to bind incorrect XML to the XML model. Fix the code that writes the file so it writes complete and correct XML and you will be able to load it into the model.

    Be aware that unless you add more code (a QFileSystemWatcher) to monitor any changes to the file, the model will not automatically reload itself if the file is changed from another program.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    Mathan (3rd October 2016)

Similar Threads

  1. Replies: 2
    Last Post: 16th May 2016, 23:50
  2. ListView - ListView communication
    By MarkoSan in forum Qt Quick
    Replies: 1
    Last Post: 30th October 2015, 10:18
  3. TWAIN Datasource in Qt
    By astodolski in forum Qt Programming
    Replies: 4
    Last Post: 19th November 2013, 21:31
  4. qml listview
    By Le_B in forum Qt Quick
    Replies: 1
    Last Post: 25th May 2011, 13:01
  5. listview
    By addu in forum Qt Programming
    Replies: 2
    Last Post: 11th May 2009, 12:05

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.