2016-03-13 33 views
0

當我試圖改變包含到一個ListModel的一個項目的屬性值下面的代碼沒有任何影響:QML:的setProperty沒有效果

Main.qml

import QtQuick 2.0 

Item { 
    anchors.fill: parent 

    ListModel { id: modelCrayon } 

    Component.onCompleted: { 
     for (var i = 0; i < 10; i++) 
      modelCrayon.append({ _tag: i, _source: "resources/crayon-green.png", _selected: false }) 
    } 

    Column { 
     x: -170 
     spacing: 0 
     Repeater { 
      model: modelCrayon 
      delegate: Crayon { 
       tag: _tag 
       source: _source 
       selected: _selected 
       onCrayonSelected: { 
        for (var i = 0; i < modelCrayon.count; i++) { 
         if (i == tag) continue; 
         modelCrayon.setProperty(i, "_selected", false); 
        } 
       } 
      } 
     } 
    } 
} 

Crayon.qml

import QtQuick 2.0 

Image { 
    property bool selected 
    property int tag 
    signal crayonSelected() 

    id: crayon 
    smooth: true 
    fillMode: Image.PreserveAspectFit 

    onSelectedChanged: console.debug(tag, selected) 

    MouseArea { 
     anchors.fill: parent 
     onClicked: { 
      selected = !selected 
      if (selected) crayonSelected() 
     } 
    } 

    states: State { 
     name: "selected"; when: selected == true 
     PropertyChanges { target: crayon; x: 30 } 
    } 

    transitions: Transition { 
     from: ""; to: "selected" 
     PropertyAnimation { property: "x"; duration: 500; easing.type: Easing.InOutQuad } 
    } 

} 

控制檯上沒有顯示任何內容,所以「selected」var始終不變。 我確定有一些明顯的我失蹤了。

順便說一下,有沒有一種更智能的方式來使用ListModel作爲OptionBox?我的意思是我只想在時間只有一個項目必須具有選定的屬性== true。或者換句話說,保持所選索引的軌道。

+0

我不知道這是否是「真實」的答案。我會說這是一種解決方法,但我解決了我的問題,在ListView中更改列並將選定的屬性設置爲ListView.isCurrentItem。 – Mark

回答

0

這是一個工作代碼,以實現我所問。但它並沒有回答爲什麼沒有設置財產。

ListView { 
     id: list 
     anchors.verticalCenter: parent.verticalCenter 
     height: parent.height 
     x: -150 
     spacing: 0 
     orientation: ListView.Vertical 
     focus: true 
     model: modelCrayon 
     delegate: Crayon { 
      id: delegate 
      source: _source 
      selected: ListView.isCurrentItem 

      MouseArea { 
       anchors.fill: parent 
       onClicked: list.currentIndex = index 
      } 
     } 
    } 
0

我測試了您的示例代碼(列版本),它適用於Qt 5.4/Windows 7 64位。

你的運行環境是什麼?

+0

使用定製的Linux buildroot環境的RPi2上的Qt 5.5 – Mark