ListView.model 不能正常工作 [英] ListView.model does not work well

查看:42
本文介绍了ListView.model 不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

这是我的完整代码.

导入QtQuick 2.0导入 FilesIO 1.0导入 QtQuick.Window 2.2导入 QtQuick.Controls 1.3导入 QtQml 2.2应用程序窗口{身份证:根宽度:320高度:410标题:LOLLLL"白颜色"可见:真实属性字符串 currentStockId: ""属性字符串 currentStockName: ""函数读取文档(){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()文件IO{id:io}列表显示 {编号:查看anchors.fill:父级宽度:parent.width剪辑:真实keyNavigationWraps: 真高亮移动持续时间:0焦点:真实snapMode:ListView.SnapToItem/* 模型:列表模型 {编号:股票//数据来自:http://en.wikipedia.org/wiki/NASDAQ-100ListElement {名称:苹果公司";stockId:苹果";值:0.0";改变:0.0";变化百分比:0.0"}ListElement {名称:Adobe Inc";stockId: "ADBE";值:0.0";改变:0.0";变化百分比:0.0"}}*/onCurrentIndexChanged:{mainRect.listViewActive = 0;root.currentStockId = model.get(currentIndex).stockId;root.currentStockName = model.get(currentIndex).name;}委托:矩形{高度:102宽度:parent.width颜色:透明"鼠标区域{anchors.fill:父级;已点击:{view.currentIndex = 索引;}}文本 {id:stockIdTextanchors.top: parent.topanchors.topMargin: 15anchors.left: parent.left锚点.leftMargin:15宽度:125高度:40颜色:#000000"font.family: "Droid Sans Georgian"font.pointSize: 20font.weight: 字体粗体垂直对齐:Text.AlignVCenter文字:stockId}文本 {id:stockValueTextanchors.top: parent.topanchors.topMargin: 15anchors.right: parent.rightanchors.rightMargin: 0.31 * parent.width宽度:190高度:40颜色:#000000"font.family: "Droid Sans Ethiopic"font.pointSize: 20font.bold: 真水平对齐:Text.AlignRight垂直对齐:Text.AlignVCenter文本:值Component.onCompleted: view.getCloseValue(index);}文本 {id:stockValueChangeTextanchors.top: parent.topanchors.topMargin: 15anchors.right: parent.rightanchors.rightMargin: 20宽度:135高度:40颜色:#328930"font.family: "Droid Sans Hebrew"font.pointSize: 20font.bold: 真水平对齐:Text.AlignRight垂直对齐:Text.AlignVCenter文字:改变onTextChanged:{如果(解析浮动(文本)> = 0.0)颜色 = "#328930";别的颜色 = "#d40000";}}文本 {id:股票名称文本anchors.top: stockIdText.bottomanchors.left: parent.left锚点.leftMargin:15宽度:330身高:30颜色:#000000"font.family:打开Sans"字体点大小:16font.bold: 假elide: Text.ElideRight最大行数:1垂直对齐:Text.AlignVCenter文字:姓名}文本 {id:stockValueChangePercentageTextanchors.top: stockIdText.bottomanchors.right: parent.rightanchors.rightMargin: 20宽度:120身高:30颜色:#328930"font.family:打开Sans"字体点大小:18font.bold: 假水平对齐:Text.AlignRight垂直对齐:Text.AlignVCenter文本:更改百分比onTextChanged:{如果(解析浮动(文本)> = 0.0)颜色 = "#328930";别的颜色 = "#d40000";}}长方形 {id: 结束行anchors.bottom: parent.bottomanchors.left: parent.left高度:1宽度:parent.width颜色:#d7d7d7"}}突出显示:矩形{宽度:parent.width颜色:#eeeeee"}}}

这里是用作输入的 stoc.json.

<预><代码>[{"name": "苹果公司","stockId":"苹果","值":"0.0","改变":"0.0",变化百分比":0.0"},{"name": "Adobe Inc","stockId":"ADBE","值":"0.0","改变":"0.0",变化百分比":0.0"},{"name": "模拟设备公司","stockId":"ADI","值":"0.0","改变":"0.0",变化百分比":0.0"},{"name": "Automatic Data Processing Inc","stockId":"ADP","值":"0.0","改变":"0.0",变化百分比":0.0"},]

解决方案

Qt 文档 你可以阅读

<块引用>

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

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

以下是如何实现转换的示例:

ListView {anchors.fill:父级模型:列表模型{id:列表模型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]);}}}委托:文本{文本:名称 + ", " + stockId + ", " + value + ", " + change + ", " + changepercentage}}

In the following code I have a ListView which I feed with JSON data. I use the parse function to extract data and assign it to the model, i.e. :

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

However, it seems that view.model don't get the data so that my application can't show anything.

Here is my full code.

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"
        }
    }
    
}

And here is the stoc.json used as input.

[
   {
      "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"
    },
]

解决方案

Looking at the Qt docs you can read that

Models can be created directly in QML using ListModel, XmlListModel or VisualItemModel, or provided by C++ model classes.

Hence, there is nothing about JSON data. You should either convert it to ListModel or provide any another suitable method.

Here is an example on how conversion can be realised:

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
    }
}

这篇关于ListView.model 不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆