【问题标题】:QML, dynamically adding elements to a listviewQML,动态地将元素添加到列表视图
【发布时间】:2013-01-20 23:13:42
【问题描述】:

我需要一些帮助来将元素添加到 qml 列表视图中,我有一个 textarea 和一个按钮,当按下时会将 textarea 文本添加到列表视图项中,这是我的尝试:

Component {
    id: delegate
    Item {
        width: 200; height: 28
        Label {
            text: score
        }
    }
}

ListView {
     id: p1scores
     model: p1model
     delegate: delegate
     anchors.top: p1name.bottom
     anchors.topMargin: units.gu(1)
}

ListModel {
     id: p1model
     ListElement { score: "0" }
}

TextArea {
     id: p1input
     width: units.gu(8)
     height: units.gu(3)
     horizontalAlignment: TextEdit.AlignHCenter
     inputMethodHints: Qt.ImhDigitsOnly
     contentHeight: units.gu(60)
     anchors.topMargin: units.gu(8)
}

Button {
     id:p1button
     text: i18n.tr("Add")
     width: units.gu(8)
     onClicked: {
        p1model.append({"score": p1input.text})
        p1input.text = ""
     }
}

我尝试添加它,但没有显示在列表视图中...有什么帮助吗?

【问题讨论】:

  • 在您的 p1button onClicked 中,您在 将文本添加到 ListModel 之前清除了 TextArea 中的文本,当然文本不会显示出来。
  • 文本仍然没有显示,我认为这是代表的问题...(编辑问题)
  • 我不确定但是...将您的 delegate 组件 ID 更改为其他内容,因为当您在 ListView 中使用 delegate: delegate 时,它假定为 delegate: p1scores.delegate,所以它绑定回来对自己。
  • 我发现了问题,是我没有指定列表视图的高度,显然默认是单个项目的大小...

标签: listview qml qtquick2


【解决方案1】:

尝试在 'score' 周围不加引号,如下所示:

onClicked: {
    p1model.append({score: p1input.text})
    p1input.text = ""
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 2016-11-22
    相关资源
    最近更新 更多