qml中数组如何操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了qml中数组如何操作相关的知识,希望对你有一定的参考价值。

用JS实现啦,例如
function GetRandomNum(Min,Max)

var Range = Max - Min;
var Rand = Math.random();
return(Min + Math.round(Rand * Range));
.
生成的数据可以转为数组,或者放入ListModel 之类的.

在QML 页面上用Repeater 或者ListView GridView 显示

例如: Qt creater 里自带的例子 painted item的QML代码
import QtQuick 2.0

Item
height: 480
width: 320

//! [0]
ListModel
id: balloonModel
ListElement
balloonWidth: 200

ListElement
balloonWidth: 120



ListView
anchors.bottom: controls.top
anchors.bottomMargin: 2
anchors.top: parent.top
id: balloonView
delegate: Rectangle
height: 50
anchors.right: index % 2 == 0 ? undefined : parent.right
width: balloonWidth
color: "green"

model: balloonModel
spacing: 5
width: parent.width

//! [0]

//! [1]
Rectangle
id: controls

anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.margins: 1
anchors.right: parent.right
border.width: 2
color: "white"
height: parent.height * 0.15

Text
anchors.centerIn: parent
text: "Add another balloon"


MouseArea
anchors.fill: parent
hoverEnabled: true
onClicked:
console.log("click!!!!!!!!!!!!")
balloonModel.append("balloonWidth": Math.floor(Math.random() * 200 + 100))
balloonView.positionViewAtIndex(balloonView.count -1, ListView.End)

onEntered:
parent.color = "#8ac953"

onExited:
parent.color = "white"



//! [1]
参考技术A 用JS实现啦,例如
function GetRandomNum(Min,Max)

var Range = Max - Min;
var Rand = Math.random();
return(Min + Math.round(Rand * Range));
.
生成的数据可以转为数组,或者放入ListModel 之类的.

在QML 页面上用Repeater 或者ListView GridView 显示

例如: Qt creater 里自带的例子 painted item的QML代码
import QtQuick 2.0

Item
height: 480
width: 320

//! [0]
ListModel
id: balloonModel
ListElement
balloonWidth: 200

ListElement
balloonWidth: 120



ListView
anchors.bottom: controls.top
anchors.bottomMargin: 2
anchors.top: parent.top
id: balloonView
delegate: Rectangle
height: 50
anchors.right: index % 2 == 0 ? undefined : parent.right
width: balloonWidth
color: "green"

model: balloonModel
spacing: 5
width: parent.width

//! [0]

//! [1]
Rectangle
id: controls

anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.margins: 1
anchors.right: parent.right
border.width: 2
color: "white"
height: parent.height * 0.15

Text
anchors.centerIn: parent
text: "Add another balloon"


MouseArea
anchors.fill: parent
hoverEnabled: true
onClicked:
console.log("click!!!!!!!!!!!!")
balloonModel.append("balloonWidth": Math.floor(Math.random() * 200 + 100))
balloonView.positionViewAtIndex(balloonView.count -1, ListView.End)

onEntered:
parent.color = "#8ac953"

onExited:
parent.color = "white"



//! [1]

如何在QT QML中替换变体数组文本

我想替换qml中的字符串文本。例如,存在QML变体数组。我用来发短信。

property variant    szString: ["", "", "", "", "", "", "", "", "", ""]

i do data in szString.
szString[1] = "abcdefg"
szString[2] = "a###b"
...

Text: {
    text: szString[index]
    ...
}

我想在这篇文章中替换字符串。例如,

i want szString[2].replace("#"," "). (i want result -- szString[2] = "a   b" )
but, the QML language can't apply.(szString.replace("#"," ") can apply, but this result isn't... i don't want result)

请帮我....

答案
while (szString[2].search("#") > -1) szString[2] = szString[2].replace("#"," ")

以上是关于qml中数组如何操作的主要内容,如果未能解决你的问题,请参考以下文章

如何在 QML 中显示二维数组的值?

如何在QT QML中替换变体数组文本

Qt5。在 QML 中嵌入 QWidget 对象

如何将 qml ScatterSeries 添加到现有的 qml 定义的 ChartView?

在 QML 中声明 JS-dicts 时如何使用 JS 数组作为键

如何使用CMake项目生成提前的QML缓存?