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

Elvis Stansvik elvstone at gmail.com
Wed Jun 8 09:49:36 CEST 2016


2016-06-08 9:46 GMT+02:00 Elvis Stansvik <elvstone at gmail.com>:
> 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...).

And even disregarding the sloppiness issue, it also forces me to do
the model = libraryModel in Component.onCompleted, so that the model
is set a second time.

Elvis



More information about the Interest mailing list