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

Elvis Stansvik elvstone at gmail.com
Mon Jun 13 13:10:39 CEST 2016


As a follow-up to my own mail: It seems the following note from the
docs applies to the initial modelChanged:

"Depending on how the model is populated, the model may not be ready
when TableView Component.onCompleted is called. In that case you may
need to delay the call to positionViewAtRow by using a Timer."

So what I'm doing in my my TableView now is:

    ....
    onModelChanged: resizeColumnsToContentsTimer.start()
    onRowCountChanged: resizeColumnsToContents()

    Timer {
        id: resizeColumnsToContentsTimer
        interval: 100
        running: false
        repeat: false
        onTriggered: parent.resizeColumnsToContents()
    }
    ...

And it seems to work OK. I know this doesn't take into account
dataChanged of the underlying model, but in my case I only have whole
row insertions/removals, no data changes.

Apart from this, I must say working with the TableView item and making
it ready for our input device (only Up, Down and Enter), and styling
it has been pretty painful :/

Elvis

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