ListView.model不能很好地工作

时间:2015-09-24 02:48:32

标签: qt qml qtquick2

在下面的代码中,我有一个ListView,我用JSON数据提供。我使用parse函数提取数据并将其分配给模型,即:

view.model = JSON.parse(io.text)  

但是,似乎view.model没有获取数据,因此我的应用程序无法显示任何内容。

这是我的完整代码。



import QtQuick 2.0
import FilesIO 1.0
import QtQuick.Window 2.2
import QtQuick.Controls 1.3
import QtQml 2.2

ApplicationWindow{
    id: root
    width: 320
    height: 410
    title:"LOLLLLL"
    color: "white"
    visible: true
    property string currentStockId: ""
    property string currentStockName: ""
    function readDocument(){
        io.source =Qt.resolvedUrl( "/home/yuhongsong/Qt/Examples/Qt-5.4/quick/demos/stocqt/content/stoc.json");
        io.read();
        view.model=JSON.parse(io.text);
    }
    Component.onCompleted: readDocument()
    FileIO{
        id:io
    }
    
    
    ListView {
        id: view
        anchors.fill: parent
        width: parent.width
        clip: true
        keyNavigationWraps: true
        highlightMoveDuration: 0
        focus: true
        snapMode: ListView.SnapToItem
        /*    model:ListModel {
              id: stocks
              // Data from : http://en.wikipedia.org/wiki/NASDAQ-100
             ListElement {name: "Apple Inc."; stockId: "AAPL"; value: "0.0"; change: "0.0"; changePercentage: "0.0"}
              ListElement {name: "Adobe Inc"; stockId: "ADBE"; value: "0.0"; change: "0.0"; changePercentage: "0.0"}
    }
    */
        
        onCurrentIndexChanged: {
            mainRect.listViewActive = 0;
            root.currentStockId = model.get(currentIndex).stockId;
            root.currentStockName = model.get(currentIndex).name;
        }
        
        delegate: Rectangle {
            height: 102
            width: parent.width
            color: "transparent"
            MouseArea {
                anchors.fill: parent;
                onClicked: {
                    view.currentIndex = index;
                }
            }
            
            Text {
                id: stockIdText
                anchors.top: parent.top
                anchors.topMargin: 15
                anchors.left: parent.left
                anchors.leftMargin: 15
                width: 125
                height: 40
                color: "#000000"
                font.family: "Droid Sans Georgian"
                font.pointSize: 20
                font.weight: Font.Bold
                verticalAlignment: Text.AlignVCenter
                text: stockId
            }
            
            Text {
                id: stockValueText
                anchors.top: parent.top
                anchors.topMargin: 15
                anchors.right: parent.right
                anchors.rightMargin: 0.31 * parent.width
                width: 190
                height: 40
                color: "#000000"
                font.family: "Droid Sans Ethiopic"
                font.pointSize: 20
                font.bold: true
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                text: value
                Component.onCompleted: view.getCloseValue(index);
            }
            
            Text {
                id: stockValueChangeText
                anchors.top: parent.top
                anchors.topMargin: 15
                anchors.right: parent.right
                anchors.rightMargin: 20
                width: 135
                height: 40
                color: "#328930"
                font.family: "Droid Sans Hebrew"
                font.pointSize: 20
                font.bold: true
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                text: change
                onTextChanged: {
                    if (parseFloat(text) >= 0.0)
                        color = "#328930";
                    else
                        color = "#d40000";
                }
            }
            
            Text {
                id: stockNameText
                anchors.top: stockIdText.bottom
                anchors.left: parent.left
                anchors.leftMargin: 15
                width: 330
                height: 30
                color: "#000000"
                font.family: "Open Sans"
                font.pointSize: 16
                font.bold: false
                elide: Text.ElideRight
                maximumLineCount: 1
                verticalAlignment: Text.AlignVCenter
                text: name
            }
            
            Text {
                id: stockValueChangePercentageText
                anchors.top: stockIdText.bottom
                anchors.right: parent.right
                anchors.rightMargin: 20
                width: 120
                height: 30
                color: "#328930"
                font.family: "Open Sans"
                font.pointSize: 18
                font.bold: false
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                text: changePercentage
                onTextChanged: {
                    if (parseFloat(text) >= 0.0)
                        color = "#328930";
                    else
                        color = "#d40000";
                }
            }
            
            Rectangle {
                id: endingLine
                anchors.bottom: parent.bottom
                anchors.left: parent.left
                height: 1
                width: parent.width
                color: "#d7d7d7"
            }
        }
        
        highlight: Rectangle {
            width: parent.width
            color: "#eeeeee"
        }
    }
    
}




以下是用作输入的stoc.json

[
   {
      "name": "Apple Inc",
      "stockId":"AAPL",
      "value":"0.0",
      "change":"0.0",
      "changepercentage":"0.0"
    },
    {
      "name": "Adobe Inc",
      "stockId":"ADBE",
      "value":"0.0",
      "change":"0.0",
      "changepercentage":"0.0"
    },
    {
      "name": "Analog Devices Inc",
      "stockId":"ADI",
      "value":"0.0",
      "change":"0.0",
      "changepercentage":"0.0"
    },
    {
      "name": "Automatic Data Processing Inc",
      "stockId":"ADP",
      "value":"0.0",
      "change":"0.0",
      "changepercentage":"0.0"
    },
]

1 个答案:

答案 0 :(得分:2)

查看Qt docs,您可以阅读

  

可以使用ListModel,XmlListModel或VisualItemModel直接在QML中创建模型,或者由C ++模型类提供模型。

因此,有关于JSON数据的 nothing 。您应该将其转换为ListModel或提供任何其他合适的方法。

以下是有关如何实现转化的示例:

ListView {
    anchors.fill: parent
    model: ListModel {
        id: listModel
        Component.onCompleted: {
            var data = [{ "name": "Apple Inc", "stockId":"AAPL", "value":"0.0", "change":"0.0", "changepercentage":"0.0" }, { "name": "Adobe Inc", "stockId":"ADBE", "value":"0.0", "change":"0.0", "changepercentage":"0.0" }, { "name": "Analog Devices Inc", "stockId":"ADI", "value":"0.0", "change":"0.0", "changepercentage":"0.0" }, { "name": "Automatic Data Processing Inc", "stockId":"ADP", "value":"0.0", "change":"0.0", "changepercentage":"0.0" }]
            for(var i = 0;i < data.length;i ++) {
                listModel.append(data[i]);
            }
        }
    }
    delegate: Text {
        text: name + ", " + stockId + ", " + value + ", " + change + ", " + changepercentage
    }
}