Your spaghetti if blocks are not the prettiest ones. You made your code totally imperative which is not necessary.
Your code can be rewritten as:
import QtQuick 2.4
import QtQuick.Controls 1.3
Item
{
width: 100
height: 650
Column
{
id: col
anchors.verticalCenter: parent.verticalCenter
spacing: 20
property int index: -1
function setPage(i)
{
col.index = i
}
MyButton
{
id: page0
selected: col.index == 0
text: "A"
onClicked: parent.setPage(0)
}
MyButton
{
id: page1
selected: col.index == 1
text: "B"
onClicked: parent.setPage(1)
}
MyButton
{
id: page2
selected: col.index == 2
text: "C"
onClicked: parent.setPage(2)
}
MyButton
{
id: page3
selected: col.index == 3
text: "D"
onClicked: parent.setPage(3)
}
Component.onCompleted: setPage(0)
}
}
import QtQuick 2.4
import QtQuick.Controls 1.3
Item
{
width: 100
height: 650
Column
{
id: col
anchors.verticalCenter: parent.verticalCenter
spacing: 20
property int index: -1
function setPage(i)
{
col.index = i
}
MyButton
{
id: page0
selected: col.index == 0
text: "A"
onClicked: parent.setPage(0)
}
MyButton
{
id: page1
selected: col.index == 1
text: "B"
onClicked: parent.setPage(1)
}
MyButton
{
id: page2
selected: col.index == 2
text: "C"
onClicked: parent.setPage(2)
}
MyButton
{
id: page3
selected: col.index == 3
text: "D"
onClicked: parent.setPage(3)
}
Component.onCompleted: setPage(0)
}
}
To copy to clipboard, switch view to plain text mode
or even shorter:
import QtQuick 2.4
import QtQuick.Controls 1.3
Item
{
width: 100
height: 650
Column
{
id: col
anchors.verticalCenter: parent.verticalCenter
spacing: 20
property int index: 0
Repeater {
model: [ "A", "B", "C", "D" ]
MyButton
{
selected: col.index == index
text: modelData
onClicked: col.index = index
}
}
}
import QtQuick 2.4
import QtQuick.Controls 1.3
Item
{
width: 100
height: 650
Column
{
id: col
anchors.verticalCenter: parent.verticalCenter
spacing: 20
property int index: 0
Repeater {
model: [ "A", "B", "C", "D" ]
MyButton
{
selected: col.index == index
text: modelData
onClicked: col.index = index
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks