QML gridview mousearea example: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
Part of http://developer.qt.nokia.com/forums/viewthread/790/ ''[developer.qt.nokia.com]''
[[Category:Developing_with_Qt::Qt Quick::Tutorial]]


===Categories:===
[[Category:Learning::Demos_and_Examples]]


* [[:Category:Developing with Qt|Developing_with_Qt]]
Part of "http://developer.qt.nokia.com/forums/viewthread/790/":http://developer.qt.nokia.com/forums/viewthread/790/
** [[:Category:Developing with Qt::Qt Quick|Qt_Quick]]
* [[:Category:Developing with Qt::Qt Quick::Tutorial|Tutorial]]


* [[:Category:Learning|Learning]]
<code>
** [[:Category:Learning::Demos and Examples|Demos_and_Examples]]
 
import Qt 4.7
 
Item {<br /> width: 400<br /> height: 400
 
// handle clicks on empty area<br /> MouseArea {<br /> anchors.fill: parent<br /> onClicked: grid.currentIndex = –1<br /> }
 
// a dummy model<br /> ListModel {<br /> id: itemModel<br /> ListElement {<br /> name: &quot;red&amp;quot;<br /> }<br /> ListElement {<br /> name: &quot;blue&amp;quot;<br /> }<br /> ListElement {<br /> name: &quot;green&amp;quot;<br /> }<br /> ListElement {<br /> name: &quot;tomato&amp;quot;<br /> }<br /> }
 
// our delegate<br /> Component {<br /> id: rectDelegate<br /> Rectangle {<br /> id: rect
 
width: 50<br /> height: 50<br /> color: name
 
MouseArea {<br /> anchors.fill: parent<br /> onClicked: grid.currentIndex = index<br /> }<br /> states: [<br /> State {<br /> name: &quot;none&amp;quot;<br /> when: (grid.currentIndex == –1)<br /> },<br /> State {<br /> name: &quot;selected&amp;quot;<br /> when: rect.GridView.isCurrentItem<br /> PropertyChanges {<br /> target: rect;<br /> width: 100;<br /> height: 100<br /> }<br /> }<br /> ]<br /> transitions: Transition {<br /> PropertyAnimation {<br /> target: rect<br /> properties: &quot;width, height&amp;quot;<br /> }<br /> }<br /> }<br /> }
 
GridView {<br /> id: grid<br /> x: 50<br /> y: 50<br /> width: 200<br /> height: 200<br /> model: itemModel<br /> delegate: rectDelegate
 
// handle clicks on empty area within the grid.<br /> // this adds an element below the grid items but on the grid's flickable surface<br /> // (so it won't have mouse events stolen by the grid)<br /> flickableChildren: MouseArea {<br /> anchors.fill: parent<br /> onClicked: grid.currentIndex = –1<br /> }
 
// sets the initial index to –1, so no item is selected<br /> // currentIndex: –1 // not enough, need to check later<br /> Component.onCompleted: currentIndex = –1<br /> }<br />}

Revision as of 09:46, 24 February 2015


Part of "http://developer.qt.nokia.com/forums/viewthread/790/&quot;:http://developer.qt.nokia.com/forums/viewthread/790/

import Qt 4.7

Item {
width: 400
height: 400

// handle clicks on empty area
MouseArea {
anchors.fill: parent
onClicked: grid.currentIndex = –1
}

// a dummy model
ListModel {
id: itemModel
ListElement {
name: "red&quot;
}
ListElement {
name: "blue&quot;
}
ListElement {
name: "green&quot;
}
ListElement {
name: "tomato&quot;
}
}

// our delegate
Component {
id: rectDelegate
Rectangle {
id: rect

width: 50
height: 50
color: name

MouseArea {
anchors.fill: parent
onClicked: grid.currentIndex = index
}
states: [
State {
name: "none&quot;
when: (grid.currentIndex == –1)
},
State {
name: "selected&quot;
when: rect.GridView.isCurrentItem
PropertyChanges {
target: rect;
width: 100;
height: 100
}
}
]
transitions: Transition {
PropertyAnimation {
target: rect
properties: "width, height&quot;
}
}
}
}

GridView {
id: grid
x: 50
y: 50
width: 200
height: 200
model: itemModel
delegate: rectDelegate

// handle clicks on empty area within the grid.
// this adds an element below the grid items but on the grid's flickable surface
// (so it won't have mouse events stolen by the grid)
flickableChildren: MouseArea {
anchors.fill: parent
onClicked: grid.currentIndex = –1
}

// sets the initial index to –1, so no item is selected
// currentIndex: –1 // not enough, need to check later
Component.onCompleted: currentIndex = –1
}
}