QML ListElement传递字符串列表

编程入门 行业动态 更新时间:2024-10-27 03:29:13
本文介绍了QML ListElement传递字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个listview,其代表中有一个中继器,该中继器应该由文本填充.如果中继器的模型属性是通过以下方式硬编码的:

I have a listview who's delegate has a repeater in it which is supposed to be populated by text. If the repeater's model property is hard coded this way:

model: ['String 1', 'String 2', 'String 3'];

通过在转发器的区域中显示3个项目,它可以完美工作. 但是,我想使用ListElement发送这样的列表,这就是我尝试过的:

It works perfectly by showing 3 items in the repeater's region. However, I want to send such a list using a ListElement and this is what I tried:

ListElement{ shape: "Circle"; colors: ['Red', 'Blue']; }

不幸的是,这种方法行不通,并引发错误:

Unfortunately this approach doesn't work and throws an error:

ListElement:不能将脚本用于属性值

ListElement: cannot use script for property value

我实现了多少? TIA

How many I achieve this? TIA

推荐答案

您不能:

值必须是简单的常量;可以是字符串(带引号,并且可以选择在调用QT_TR_NOOP的情况下),布尔值(真,假),数字或枚举值(例如AlignText.AlignHCenter).

Values must be simple constants; either strings (quoted and optionally within a call to QT_TR_NOOP), boolean values (true, false), numbers, or enumeration values (such as AlignText.AlignHCenter).

向视图公开数据的最强大方法是创建 C ++模型.

The most powerful way to expose data to views is by creating a C++ model.

但是,如果您真的不想使用C ++,则可以将颜色存储在以逗号分隔的字符串中,然后将它们分开:

However, if you really don't want to go to C++, you could store the colours in a comma-separated string, and then split them:

import QtQuick 2.6 import QtQuick.Window 2.0 Window { visible: true width: 200 height: 200 ListView { width: 32 height: 64 anchors.centerIn: parent model: ListModel { ListElement{ shape: "Circle" colors: "red, blue" } ListElement{ shape: "Square" colors: "green,yellow" } } delegate: Rectangle { width: 32 height: 32 radius: shape === "Circle" ? width / 2 : 0 property var colorArray: colors.split(",") gradient: Gradient { GradientStop { position: 0 color: colorArray[0] } GradientStop { position: 1 color: colorArray[1] } } } } }

更多推荐

QML ListElement传递字符串列表

本文发布于:2023-11-24 08:28:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624475.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   列表   QML   ListElement

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!