Select

Description

The Select object is a PropertyModifier object which reduces a source list to a single value using an arbitrary expression. The expression is evaluated for each element in the source list and the final result written to the target property. Select manages strong bindings between elements in the source list, the expression and the reduced value, i.e. whenever either an element in the source list or other variable parts of the evaluated expression change the target property is being updated instantly.

Select primarily is designed to use with DataObject lists. Select replaces traditional for loops in a declarative manner which often allows simplifying the code and thus reduce its complexity.

› Inherits:PropertyModifier

Properties

defaultValue

This property holds a value to write to the target in case select does not evaluate to true for any source list element.

This property was introduced in InCore 2.0.

› Type:Variant
› Signal:defaultValueChanged()
› Attributes:Writable

error

This property holds the most recently occurred error or Select.NoError if no error occurred. If the same error occurs multiple times this property does not change. Use the errorOccurred() signal to detect multiple occurrences of the same error.

› Type:Error
› Signal:errorChanged()
› Attributes:Readonly

errorString

This property holds the current human readable error string corresponding to the current value in the error property. It may include additional information such as failure reasons or locations.

› Type:String
› Signal:errorStringChanged()
› Attributes:Readonly

eval

This property holds an expression which is evaluated for the selected element in the source list. The respective source list element is available in a local item property inside the expression. This allows transforming the selected item or select subproperties. If unset the item itself is written to the target.

› Type:<QML expression>
› Signal:evalChanged()
› Attributes:Writable, Optional

select

This property holds an expression which is used to select a list item. The expression is evaluated for each item and needs to evaluate to true for the particular item to select and write to the target.

› Type:<QML expression>
› Signal:selectChanged()
› Attributes:Writable

source

This property holds a reference to an arbitrary object list or value List which to select an item from.

› Type:<QML expression>
› Signal:sourceChanged()
› Attributes:Writable

Signals

errorOccurred()

This signal is emitted whenever an error has occurred, regardless of whether the error property has changed or not. In contrast to the change notification signal of the error property this signal is also emitted several times if a certain error occurs several times in succession.

Enumerations

Error

This enumeration describes all errors which can occur in Select objects. The most recently occurred error is stored in the error property.

Name Value Description
Select.NoError 0 No error occurred or was detected.
Select.InvalidSource 1 Operation not supported for empty or non-list source.
Select.ExpressionError 2 Error while evaluating select or eval expression: <Unknown File>: .
Select.TargetWriteError 3 The result value could not be written to the target property, likely due to type incompatibilities.

Example

import InCore.Foundation 2.5

Application {

    List {
        id: simpleValueList
        items: [ 10, 20, 30 ]
    }

    property int simpleValue
    Select on simpleValue {
        source: simpleValueList
        select: item > 20
    }

    onSimpleValueChanged: console.log("Selected simple value:", simpleValue)

    MeasurementGroup {
        id: measurements
        Measurement { name: "A"; data: 1 }
        Measurement { name: "B"; data: 2 }
        Measurement { name: "C"; data: 3 }
        Measurement { name: "D"; data: 4 }
        Measurement { name: "E"; data: 5 }
    }

    property int measurementValue
    Select on measurementValue {
        source: measurements.objects
        select: item.name === "C"
        eval: item.data
    }

    onMeasurementValueChanged: console.log("Selected measurement value:", measurementValue)

    property var threshold: 3
    property bool anyMeasurementExceedsThreshold: false

    Select on anyMeasurementExceedsThreshold {
        defaultValue: false
        source: measurements.objects
        select: item.data > threshold
        eval: true
    }

    onAnyMeasurementExceedsThresholdChanged: {
        if(anyMeasurementExceedsThreshold)
            console.log("There's at least one measurement above", threshold)
        else
            console.log("There's no measurement above", threshold)
    }

    Timer {
        onTriggered: threshold++
    }
}