catinred
catinred

Reputation: 329

QT/QML:How to show a message when the model has no data

I've implement a simple Model View app, when there is no data in the model, the ListView is just a blank form. I want to know how to show a convenient message,telling that the model has no data. Thank you.

Upvotes: 2

Views: 4557

Answers (2)

muesli
muesli

Reputation: 815

At least with QtQuick2 you can do something like this:

import QtQuick 2.9
import QtQuick.Controls 2.2

ListView {
    model: ...
    clip: true

    Label {
        anchors.fill: parent
        horizontalAlignment: Qt.AlignHCenter
        verticalAlignment: Qt.AlignVCenter
        visible: parent.count == 0
        text: qsTr("Nothing to show yet!")
        font.bold: true
    }
}

Upvotes: 1

Abhijith
Abhijith

Reputation: 2642

Overlay a listview and a text element on top of each other. Set visibilty to true or false depending on model.count

ListView{
     visible : if(model.count > 0) true;else false;
}
Text{
     visible : if(model.count > 0) false;else true;
}

Upvotes: 1

Related Questions