I’m developing a small application, and would like to change the number of columns in a grid view depending on the device’s screen size and rotation.
This is what I have so far, but it misbehaves on devices where some orientations are not allowed (most commonly reverse-portrait). The orientation is detected and the cellWidth is changed, but the screen is - of course - not rotated to that orientation (because it’s not allowed).
Any idea how I would correct that, so that it doesn’t change the cellWidth if the new rotation is not allowed?
import QtQuick 2.2
import Sailfish.Silica 1.0
Page {
SilicaGridView{
anchors.fill: parent
cellWidth: {
if (Screen.sizeCategory === 1) {
// Phone
if (app.deviceOrientation === 1 || app.deviceOrientation === 4) {
//Portrait
return width;
} else {
//app.deviceOrientation === 2
//app.deviceOrientation === 8
//Landscape
return width/2;
}
}
if (Screen.sizeCategory === 2) {
// Tablet
if (app.deviceOrientation === 1 || app.deviceOrientation === 4) {
//Portrait
return width/2;
} else {
//app.deviceOrientation === 2
//app.deviceOrientation === 8
//Landscape
return width/3;
}
}
//Fallback to one column
return width;
}
}
}