qt - How to validate Qml property value before assigning? -


element.qml

item{     property int step     property int minvalue     property int maxvalue     property int value  //should accept values of form i*step+minvalue } 

main.qml

element{     step:2     minvalue:-7     maxvalue:7     value:0  //not acceptable  value(2*i-7), should convert 1 or -1 } 

if set value, should convert value nearest step*i+minvalue, i.e. 0 1 , emit valuechanged().

i want valuechanged() signal emitted when value step*i+minvalue.

you cant pre test , on valuechanged signal sent. solution test in onvaluechanged slot , send custom signal, not send valuechanged() sigal cause it's connected onvaluechanged slot.

import qtquick 2.2 import qtquick.layouts 1.1 import qtquick.controls 1.2  item{     property int step :2     property int minvalue :-7     property int maxvalue: 7     property int value: 0  //should accept values of form i*step+minvalue      signal sgnvaluechanged(int value)      column{          textinput{             id:input             width: 100             height: 20         }          rectangle{             id:button             width: 100             height: 20             color:"#f0ffff"             mousearea{                 anchors.fill:parent                 onclicked:                 {                     value = parseint(input.text)                  }             }         }     }      onvaluechanged:     {         if(value >= minvalue && value <= maxvalue)         {             sgnvaluechanged(step * value + minvalue)         }          else         {             console.log("not acceptable value")         }     } } 

Comments