Connect SpinBox to Slider (Dynamic Qml) [SOLVED]



  • Hi, guys.
    Here is a simple of dynamically creating many pairs of SpinBox and Sliders, I want to connect the value of each SpinBox to the value of it's corresponding Slider.

    @
    import QtQuick 2.3
    import QtQuick.Controls 1.2

    ApplicationWindow {
    visible: true
    width: 400
    height: 300
    color: Qt.rgba(0, 0, 0, 1)

    function createControl(itemType, where)
    {
        return Qt.createQmlObject("import QtQuick.Controls 1.2; "
                                  + itemType
                                  + " {}",
                                  where,
                                  "ApplicationWindow")
    }
    
    Component.onCompleted: {
        // Create some controls.
        for (var i = 0; i < 10; i++) {
            var slider = createControl("Slider", grid)
            var spinBox = createControl("SpinBox", grid)
    
            // Connect the SpinBox to the Slider.
            // This is what I want but doesn't work.
            
            // slider.valueChanged.connect(spinBox.setValue)
            // spinBox.valueChanged.connect(slider.setValue)
        }
    }
    
    Grid {
        id: grid
        columns: 2
        anchors.fill: parent
    }
    

    }
    @

    Some ideas?



  • Ok, solved. Here is the code:

    @
    import QtQuick 2.3
    import QtQuick.Controls 1.2

    ApplicationWindow {
    id: appWindow
    visible: true
    width: 400
    height: 300
    color: Qt.rgba(0, 0, 0, 1)

    property var sliders: []
    property var spinBoxes: []
    
    function createControl(itemType, where)
    {
        return Qt.createQmlObject("import QtQuick.Controls 1.2; "
                                  + itemType
                                  + " {}",
                                  where,
                                  "ApplicationWindow")
    }
    
    function spinBoxChanged()
    {
        for (var spinBox in appWindow.spinBoxes)
            appWindow.sliders[spinBox].value =
                    appWindow.spinBoxes[spinBox].value
    }
    
    function sliderChanged()
    {
        for (var sliders in appWindow.sliders)
            appWindow.spinBoxes[sliders].value =
                    appWindow.sliders[sliders].value
    }
    
    Component.onCompleted: {
        // Create some controls.
        for (var i = 0; i < 10; i++) {
            var slider = createControl("Slider", grid)
            var spinBox = createControl("SpinBox", grid)
    
            // Connect the SpinBox to the Slider.
            appWindow.sliders.push(slider)
            appWindow.spinBoxes.push(spinBox)
    
            slider.onValueChanged.connect(appWindow.sliderChanged)
            spinBox.onValueChanged.connect(appWindow.spinBoxChanged)
        }
    }
    
    Grid {
        id: grid
        columns: 2
        anchors.fill: parent
    }
    

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.