Part of http://developer.qt.nokia.com/forums/viewthread/790/ [developer.qt.nokia.com]

  1. import Qt 4.7
  2.  
  3. Item {
  4.     width: 400
  5.     height: 400
  6.  
  7.     // handle clicks on empty area
  8.     MouseArea {
  9.         anchors.fill: parent
  10.         onClicked: grid.currentIndex = -1
  11.     }
  12.  
  13.     // a dummy model
  14.     ListModel {
  15.         id: itemModel
  16.         ListElement {
  17.             name: "red"
  18.         }
  19.         ListElement {
  20.             name: "blue"
  21.         }
  22.         ListElement {
  23.             name: "green"
  24.         }
  25.         ListElement {
  26.             name: "tomato"
  27.         }
  28.     }
  29.  
  30.     // our delegate
  31.     Component {
  32.         id: rectDelegate
  33.         Rectangle {
  34.             id: rect
  35.  
  36.             width: 50
  37.             height: 50
  38.             color: name
  39.  
  40.             MouseArea {
  41.                 anchors.fill: parent
  42.                 onClicked: grid.currentIndex = index
  43.             }
  44.             states: [
  45.                 State {
  46.                     name: "none"
  47.                     when: (grid.currentIndex == -1)
  48.                 },
  49.                 State {
  50.                     name: "selected"
  51.                     when: rect.GridView.isCurrentItem
  52.                     PropertyChanges {
  53.                         target: rect;
  54.                         width: 100;
  55.                         height: 100
  56.                     }
  57.                 }
  58.             ]
  59.             transitions: Transition {
  60.                 PropertyAnimation {
  61.                     target: rect
  62.                     properties: "width, height"
  63.                 }
  64.             }
  65.         }
  66.     }
  67.  
  68.     GridView {
  69.         id: grid
  70.         x: 50
  71.         y: 50
  72.         width: 200
  73.         height: 200
  74.         model: itemModel
  75.         delegate: rectDelegate
  76.  
  77.         // handle clicks on empty area within the grid.
  78.         // this adds an element below the grid items but on the grid's flickable surface
  79.         //     (so it won't have mouse events stolen by the grid)
  80.         flickableChildren: MouseArea {
  81.             anchors.fill: parent
  82.             onClicked: grid.currentIndex = -1
  83.         }
  84.  
  85.         // sets the initial index to -1, so no item is selected
  86.         //        currentIndex: -1 // not enough, need to check later
  87.         Component.onCompleted: currentIndex = -1
  88.        }
  89. }

Categories:

  • Learning
  •