go-qml / qml

QML support for the Go language
Other
1.96k stars 187 forks source link

qml.Changed works only when the value really change #45

Closed laurent22 closed 10 years ago

laurent22 commented 10 years ago

I'm trying to make a simple file manager with the qml package. I have this view at the moment, which displays a list of files:

import QtQuick 2.0
import QtQuick.Controls 1.1

ApplicationWindow {
    objectName: "mainWindow"
    title: "My Application"
    width: 800
    height: 600

    TableView {
        objectName: "fileView"
        TableViewColumn{ role: "Name"  ; title: "Name" ; width: 600 }
        TableViewColumn{ role: "Size" ; title: "Size" ; width: 200 }
        model: files.len
        itemDelegate: Item {
            Text {
                text: files.cell(styleData.row, styleData.column)
            }
            clip: true
        }
        width: 800
        height: 600
        selectionMode: 2
    }
}

From Go, when the model changes, I call qml.Changed(files, &files.Len). This works well if the number of files before and after is different, however it does not do anything if files.Len hasn't actually changed.

Is this expected behavior? If so, is there any other way to force the view to refresh based on the model?

niemeyer commented 10 years ago

Yes, that behavior is sane and should be expected. The model may cache the length internally and be smart about the fact the length hasn't in fact changed, so there's nothing to do as far as that information is concerned.

The proper way to handle this is to return a value for which you can call Changed on the data that really changed. This will also prevent spurious redrawing for content that wasn't modified.

Here is a hacked version of the delegate example that shows this working: 71378c3d