ListView.model 不能正常工作
Posted
技术标签:
【中文标题】ListView.model 不能正常工作【英文标题】:ListView.model does not work well 【发布时间】:2015-09-24 02:48:32 【问题描述】:在下面的代码中,我有一个 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】:看看Qt docs你可以读到
模型可以使用 ListModel、XmlListModel 或 VisualItemModel 在 QML 中直接创建,也可以由 C++ 模型类提供。
因此,没有关于 JSON 数据。您应该将其转换为 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
【讨论】:
也许引用JSONListModel会很有趣。 谢谢你的回答。效果很好。但我有一个问题。为什么 tableview::model 可以直接使用 json 数据。而 listview 不能。 两个视图都对模型使用相同的方法。当TableView
使用 JSON 数据时,您发布了一个示例?
tableView works with data。我从中得到了想法^-^
function readDocument() io.source = openDialog.fileUrl io.read() view.model = JSON.parse(io.text) 摘自网页
以上是关于ListView.model 不能正常工作的主要内容,如果未能解决你的问题,请参考以下文章