[Interest] TableView has no rows/columns when initial modelChanged is emitted, bug?

Elvis Stansvik elvstone at gmail.com
Wed Jun 8 09:46:11 CEST 2016


Hi all,

Consider the following:

import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.3

Window {
    width: 500
    height: 500

    ListModel {
        id: libraryModel
        ListElement {
            title: "A Masterpiece"
        }
    }

    TableView {
        model: libraryModel
        anchors.fill: parent

        onModelChanged: {
            console.log('model changed');
            console.log('rowCount: ' + rowCount);
            console.log('columnCount: ' + columnCount);
        }

        Component.onCompleted: model = libraryModel

        TableViewColumn {
            role: "title"
            title: "Title"
        }
    }
}

This will give the output:

qml: model changed
qml: rowCount: 0
qml: columnCount: 0
qml: model changed
qml: rowCount: 1
qml: columnCount: 1

because for some reason, when modelChanged is initially emitted, the
view has no rows/columns yet, but when it is subsequently set (in
Component.onCompleted), it has. I find this quite strange, and it
prevents me from doing e.g. getColumn(0).resizeToContents() in
onModelChanged without checking if there's a column (I know that is
sloppy anyway, since the model may have been set to null, but
still...).

Was this behavior intended? I mean, the model has been set (the signal
name is in past tense), so the view should have rows/columns
corresponding to that model at that point?

Thanks in advance,
Elvis



More information about the Interest mailing list