Inujima,
Japan
2010-04-30Kobe,
Japan
2010-05-01Oslo
2010-12-03Kyoto
2011-05-04
Shimogamo
Jinja,
Jikishinkageryu
NaginatajutsuMinatogawa
Jinja,
Kobe
2011-06-20Kuortti.2010-06-26Saaristoleiri-Uto-2008_177South.Africa.Roadtrip.2007-08_353Kuortti.2010-06-26Helsinki2005-10-15_16_042Nanbudo_WCh2006_076Oslo
2010-12-04Kobujutsu
Stadion
2011-12-17
2Slovenia2009-04_658Naginata
Helsinki
2010-02-07
09South.Africa.Roadtrip.2007-08_216Nanbudo_Norge2007-12_038Vesa-Rauttu-60v_1899Kuortti.2010-06-26italia2005-04_219Shiga,
Japan
2010-04-18Hannan-ja-Esan-haat.20100822.131652.703
An angel on a grave stone

A young angel guarding on a grave stone in the Hietalahti Cemetery, Helsinki, Finland

Quick ternary use for data binding

Recently I have started to use a lot (alot?) more the ternary operation with the growing amount of QML I write on daily basis.

It provides an easy shortcut while defining the values of properties for an example. In order to have an actual code example, below is a snippet of a ListModel which has consistently the name property, but only in the last one the other property.

ListModel {
    id: itemModel
    ListElement {
        name: "Apple"
    }
    ListElement {
        name: "Fig"
    }
    ListElement {
        name: "Orange"
    }
    ListElement {
        name: "Banana"
        other: "Potato"
    }
    // Only the last item has the value for "other"...
}

The delegate which is presenting each of the ListElements will now have to take care of checking that the property does exist.

Component {
    id: itemDelegate
    Rectangle {
        Text {
            anchors.centerIn: parent
            color: "white"
            font.bold: true
            font.pointSize: 26

            // Property value is binded to a role defined in ListModel
            text: (other ? other : name)
        }
    }
}

The text will have the value of name in all others except the last one where is will use the value of other.

These two QML elements, model and delegate, could be used with ListView, GridView and PathView, non exclusively. Below a simple example of how ListView could be set.

ListView {
    id: itemList
    anchors.fill: parent
    anchors.margins: 20
    spacing: 4
    clip: true

    model: itemModel

    delegate: itemDelegate
}

See where ternary was used?

Time: 10/11/2010 10:12

QR code for paazio.nanbudo.fi