Code updated after comment received from "mich", now code looks much better.
---------------------
Now that I have some free time and no work to do, I thought to play with QML and created simple demo that implement views and changes view on swipe event.
As of now I don't have latest QML installed, therefor I am not able to use GestureArea QML element to detect swipe gesture and decided to implement my own swipe detection code in QML.
In demo, view is quite simple just showing colored rectangle and string. My intention for demo was to create reusable view element and to change view from one to another.
My view code is as below, I am calling it as screen in code and named QML file as Screen.qml. Mainly screen element maintain states to hide and show view and animate the transition. In real world it might required lots of other property and function but my implementation have only few.
Overall I felt there is lot more to do to create truly reusable view code,but this is all for now.
---------------------
Now that I have some free time and no work to do, I thought to play with QML and created simple demo that implement views and changes view on swipe event.
As of now I don't have latest QML installed, therefor I am not able to use GestureArea QML element to detect swipe gesture and decided to implement my own swipe detection code in QML.
In demo, view is quite simple just showing colored rectangle and string. My intention for demo was to create reusable view element and to change view from one to another.
My view code is as below, I am calling it as screen in code and named QML file as Screen.qml. Mainly screen element maintain states to hide and show view and animate the transition. In real world it might required lots of other property and function but my implementation have only few.
import Qt 4.7
Item {
id: screen
property string color: "red"
property string title: "title"
opacity: 1
function hide() {
screen.state = 'hide';
screen.x = 0;
}
function show(xVal) {
screen.x = xVal;
screen.state = 'show';
}
Rectangle {
id: rect
width: 480
height: 640
color: screen.color
}
Text {
id: title
text: screen.title;
}
states: [
State {
name: "show"
PropertyChanges {
target: screen
x: 0
opacity:1
}
},
State {
name: "hide"
PropertyChanges {
target: screen
opacity:0
}
}
]
transitions: [
Transition {
from:"hide"
to:"show"
NumberAnimation { properties: "x"; duration:500}
NumberAnimation { properties: "opacity"; duration: 700 }
},
Transition {
//from: "show"
to: "hide"
NumberAnimation { properties: "opacity"; duration: 700 }
}
]
}
In my main.qml entry qml code, I have swipe detection code and view switching code as below.import Qt 4.7
Rectangle {
id: container
width: 480
height: 640
property int currentScreen: 0
property int previousScreen: 0
//creating array of screens
property list<item> screens: [
Screen {
parent: container
id: firstScreen
title:"First Screen"
color: "blue"
},
Screen {
parent: container
id: secondScreen
title:"Second Screen"
color: "red"
},
Screen {
parent: container
id:thirdScreen
title:"Third Screen"
color:"green"
},
Screen {
parent: container
//anchors.fill: parent
id:fourthScreen
title:"Fourth Screen"
color:"orange"
}
]
Component.onCompleted: {
console.log("Startup script");
container.currentScreen = 0;
container.previousScreen = 0;
for(var i=0; i < 4; ++i) {
screens[i].hide();
}
screens[0].show(0);
}
// function to show particular view
function showScreen(screenNo,direction) {
screens[previousScreen].hide();
var xVal = direction == -1 ? 400 : -400;
screens[screenNo].show(xVal);
}
// function to switch view on swipe
function onLeftSwipe() {
previousScreen = currentScreen;
currentScreen = currentScreen +1;
if(currentScreen > 3 ) {
currentScreen = 0;
}
showScreen (currentScreen,-1) ;
}
function onRightSwipe() {
previousScreen = currentScreen;
currentScreen = currentScreen -1;
if(currentScreen < 0 ) {
currentScreen = 3;
}
showScreen (currentScreen,1) ;
}
// swipe detection code
MouseArea {
id: mouseArea
anchors.fill: parent;
property int oldX: 0
property int oldY: 0
onPressed: {
oldX = mouseX;
oldY = mouseY;
}
onReleased: {
var xDiff = oldX - mouseX;
var yDiff = oldY - mouseY;
if( Math.abs(xDiff) > Math.abs(yDiff) ) {
if( oldX > mouseX) {
onLeftSwipe();
} else {
onRightSwipe();
}
} else {
if( oldY > mouseY) {/*up*/ }
else {/*down*/ }
}
}
}
}
Overall I felt there is lot more to do to create truly reusable view code,but this is all for now.
















































