Как правильно установить видимый флаг для элемента Text в QML на основе счетчика моделей ListView? - PullRequest
0 голосов
/ 10 июля 2019

У меня есть следующий код 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 {}
        }
    }
}

Модель может иногда опустошаться.Когда он не пустой, я хотел бы показать текстовый элемент, в противном случае я хочу скрыть его.Но почему-то он становится видимым всегда, когда у меня есть visible: (dummyModel.count!==0)?true:false или никогда, когда у меня есть visible: (dummyModel.count>0)?true:false.

Когда модель не пуста, это выглядит следующим образом.

enter image description here

Но когда модель не пуста, текстовый элемент не скрывается.

enter image description here

Не могли бы выподскажите пожалуйста что я делаю не так?

спасибо.

1 Ответ

2 голосов
/ 11 июля 2019

Существует также свойство 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 {}
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...