QML ListElement传递字符串列表 [英] QML ListElement pass list of strings

查看:829
本文介绍了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传递字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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