Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Connect SpinBox to Slider (Dynamic Qml) [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Connect SpinBox to Slider (Dynamic Qml) [SOLVED]

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hipersayan_x
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hipersayan_x
      wrote on last edited by
      #2

      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
      }
      

      }
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved