2015-10-15 5 views
0

Я определил один слайдер и один TextInput. То, что я хочу сделать, - это когда пользователь перемещает маркер ползунка, обновляется значение в TextInput и, наоборот, когда пользователь вводит значение в TextInput, ручка ползунка обновляется.Связывание двух свойств значение

Что я здесь сделал, когда пользователь перемещает ползунок в значение TextInput, но инвертор не работает.

ColorSlider.qml

// Vertical "slider" control used in colorpicker 
import QtQuick 2.4 

Item { 
property alias pPickerCursor: pickerCursor 
property real value: (1 - pickerCursor.y/height) 

width: 15; height: 300 
Item { 
    id: pickerCursor 
    width: parent.width 
    Rectangle { 
     x: -3; y: -height*0.5 
     width: parent.width + 4; height: 7 
     border.color: "black"; border.width: 1 
     color: "transparent" 
     Rectangle { 
      anchors.fill: parent; anchors.margins: 2 
      border.color: "white"; border.width: 1 
      color: "transparent" 
     } 
    } 
} 
MouseArea { 
    id: pickerCursorMouseArea 
    anchors.fill: parent 
    function handleMouse(mouse) { 
     if (mouse.buttons & Qt.LeftButton) { 
      pickerCursor.y = Math.max(0, Math.min(height, mouse.y)) 
     } 
    } 
    onPositionChanged: { 
     handleMouse(mouse) 
    } 

    onPressed: { 
     handleMouse(mouse) 
    } 
} 
} 

NumberBox.qml

// Edit box (with caption), editing a number value 
import QtQuick 2.4 

Row { 
property alias caption: captionBox.text; 
property alias value: inputBox.text; 
property alias min: numValidator.bottom; 
property alias max: numValidator.top; 
property alias decimals: numValidator.decimals; 
property double pMax: 1; 

width: 80; 
height: 15 
spacing: 4 
anchors.margins: 2 

Text { 
    id: captionBox 
    width: 18;// height: parent.height 
    color: "#AAAAAA" 
    font.pixelSize: 11; font.bold: true 
    horizontalAlignment: Text.AlignRight; verticalAlignment: Text.AlignBottom 
    anchors.bottomMargin: 3 
} 

TextInput { 
     id: inputBox 
     anchors.leftMargin: 4; anchors.topMargin: 1; anchors.fill: parent 
     color: "#AAAAAA"; selectionColor: "#FF7777AA" 
     font.pixelSize: 11    
     maximumLength: 10 
     focus: true 
     selectByMouse: true 
     validator: DoubleValidator { 
      id: numValidator 
      bottom: 0; top: 1; decimals: 2 
      notation: DoubleValidator.StandardNotation; 
     } 

    } 
} 

main.qml

Rectangle 
{ 
width: 400; 
height: 400; 

ColorSlider { id: alphaSlider; anchors.fill: parent;} 

NumberBox { 
    id: alphaBox 
    caption: "A:"; value: Math.ceil(alphaSlider.value*255) 
    min: 0; max: 255; pMax: 255; 

    onValueChanged:{ 
    if(parseFloat(value) > pMax) 
    { 
     value = Qt.binding(function() { return Math.ceil(alphaSlider.value*255) }) 
    } 
} 
} 

ответ

0

Ну, я думаю, что вместо TextInput элемента, вы действительно должны использовать SpinBox который на самом деле предназначен для чисел и поддерживает ввод с клавиатуры в виде w флигель.

Item { 
     id: main 
     property int value : 50 

     Row { 
      Slider { 
       minimumValue: 0 
       maximumValue: 100 
       value: main.value 
       onValueChanged: main.value = value 
      } 

      SpinBox { 
       minimumValue: 0 
       maximumValue: 100 
       value: main.value 
       onValueChanged: main.value = value 
      } 

      TextInput { 
       validator: IntValidator { 
        bottom: 0 
        top: 100 
       } 
       text: main.value 
       onTextChanged: main.value = Number(text) 
      } 
     } 
    } 

Работает так, как ожидается, изменение значения с использованием каждого из элементов изменяет значения для всех элементов.

Смежные вопросы