如何基于ListView的模型计数为QML中的Text项目正确设置可见标记?

时间:2019-07-10 15:31:05

标签: qt qml

我有以下QML代码,其中基于从C ++ AbstractListModel提取的模型显示列表视图。

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.12
import QtQuick.Controls.Styles 1.4
import HackNews 1.0
import QtWebEngine 1.8

Item {
    width: 640
    height: 480

    property alias dummyModel: kidsListView.model

    ListView {
        width: 100;
        id: listView
        anchors.left: parent.left
        anchors.leftMargin: 20
        anchors.top: parent.top
        anchors.topMargin: 20
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 20
        model: HackNewsModel
        delegate: Rectangle
        {
            width: 100; height: 40

            Button {
                id: pushButton
                anchors.fill: parent
                anchors.margins:
                {
                    right: 5
                    left: 5
                    top: 5
                    bottom: 5
                }

                text: "id "+model.id
                style: ButtonStyle{
                    background: Rectangle {
                        implicitWidth: 100
                        implicitHeight: 25
                        border.width: control.activeFocus ? 2 : 1
                        border.color: "#888"
                        radius: 4
                        gradient: Gradient {
                            GradientStop { position: 0 ; color: control.pressed ? "#ccc" : "#eee" }
                            GradientStop { position: 1 ; color: control.pressed ? "#aaa" : "#ccc" }
                        }
                    }
                }

                onClicked:
                {
                    dummyModel = HackNewsModel.get(model.listId).kids
                }
            }
        }
    }

    Rectangle
    {
        id: rectangle
        anchors.left: listView.right
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        anchors.top: parent.top
        anchors.margins:
        {
            left: 20
            right: 20
            top: 20
            bottom: 20
        }
        Text {
            id: kidsHeader
            anchors.top: parent.top
            anchors.left: parent.left
            width: 100
            height: 20
            visible: (dummyModel.count!==0)?true:false
            text: "kids"
        }
        ListView {
            anchors.top: kidsHeader.bottom
            anchors.left: parent.left
            id: kidsListView
            anchors.leftMargin: 10
            model: ListModel{}
            height: 50
            delegate: Text {
                text: modelData
            }
            ScrollBar.vertical: ScrollBar {}
        }
    }
}

模型有时可以清空。当它不为空时,我想显示一个Text项,否则我想将其隐藏。但是无论如何,只要我拥有visible: (dummyModel.count!==0)?true:false,它就会永远可见,而visible: (dummyModel.count>0)?true:false则永远不会可见。

模型不为空时,其外观如下所示。

enter image description here

但是当模型不为空时,文本项不会被隐藏。

enter image description here

你能告诉我我在做什么错吗?

谢谢。

1 个答案:

答案 0 :(得分:2)

count上也有一个属性ListView,您应该使用该属性:

    Text {
        id: kidsHeader
        anchors.top: parent.top
        anchors.left: parent.left
        width: 100
        height: 20
        visible: kids_listview.count !== 0     //Important change here
        text: "kids"
    }
    ListView {
        id: kids_listview
        anchors.top: kidsHeader.bottom
        anchors.left: parent.left
        id: kidsListView
        anchors.leftMargin: 10
        model: ListModel{}
        height: 50
        delegate: Text {
            text: modelData
        }
        ScrollBar.vertical: ScrollBar {}
    }