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. Depending on the user input, create some sliders, rectangles etc.
Forum Updated to NodeBB v4.3 + New Features

Depending on the user input, create some sliders, rectangles etc.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 771 Views
  • 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.
  • F Offline
    F Offline
    fgfgfg
    wrote on last edited by
    #1

    Hey there,

    I am beginner to Qt Quick. I am wondering, is it possible to create custom number of sliders, rectangles, dials etc. depending on the user input. For example, when user writes 5 in a text box, I want to create 5 sliders and let the user to place them in anywhere on the app screen. Thanks in advance.

    DiracsbracketD 1 Reply Last reply
    0
    • F fgfgfg

      Hey there,

      I am beginner to Qt Quick. I am wondering, is it possible to create custom number of sliders, rectangles, dials etc. depending on the user input. For example, when user writes 5 in a text box, I want to create 5 sliders and let the user to place them in anywhere on the app screen. Thanks in advance.

      DiracsbracketD Offline
      DiracsbracketD Offline
      Diracsbracket
      wrote on last edited by Diracsbracket
      #2

      Hi @fgfgfg
      Have a look at dynamic object creation:
      http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html

      In its simplest form, e.g. for Rectangle objects, you could have something like this:

      First, instantiate a component, either using the Component type as in the example below, or via Qt.createComponent() as in the Qt doc.

      Component {
          id: rectComponent
          Rectangle {
             width: 200
             height: 50
          }
      }
      

      Then, using JavaScript, you can create objects from the component, e.g. as follows:

      onClicked: {
             for (var i=0; i<parseInt(textInput.text); ++i) {
                  rectComponent.createObject(visualParent, {"x": 100, "y": i*300})
             }
      }
      

      where visualParent is the visual parent item under which you want to create the objects (e.g. the id of your MainWindow)

      As stated in the doc, it may be more efficient to pass object parameters (x and y for example) directly in the call to `createObject()' rather than to set them after the object is created.

      Since you want move the objects around, you must enable drag & drop on your object.
      I used this example to learn about drag/drop in QML:
      http://doc.qt.io/qt-5/qtquick-draganddrop-example.html

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fgfgfg
        wrote on last edited by
        #3

        Hi @Diracsbracket
        Thank you for your reply.

        Now, depending on the user input I am creating some objects. For example, when i=2, it creates 2 objects. i = 4, it creates 4 objects.

        //main.qml
        Component.onCompleted: {
            button.clicked.connect (MyScript.createSpriteObjects) }
        

        But when i=3 (less than the previous i), there is still 4 objects. I could not destroy the previous objects.

        //JS File
        var component;
        var sprite;
        var i;
        
        function createSpriteObjects() {
        
            for (var i=0; i<parseInt(textField.text); i++){
            component = Qt.createComponent("Sprite.qml");
            sprite = component.createObject(appWindow, {"x": 400, "y": i*100});
            }
        
            if (sprite === null) {
                // Error Handling
                console.log("Error creating object");
            }
        }
        

        Is there any way to do that instead of defining another button and sending signal to destroy function. Thanks in advance.

        DiracsbracketD 1 Reply Last reply
        0
        • F fgfgfg

          Hi @Diracsbracket
          Thank you for your reply.

          Now, depending on the user input I am creating some objects. For example, when i=2, it creates 2 objects. i = 4, it creates 4 objects.

          //main.qml
          Component.onCompleted: {
              button.clicked.connect (MyScript.createSpriteObjects) }
          

          But when i=3 (less than the previous i), there is still 4 objects. I could not destroy the previous objects.

          //JS File
          var component;
          var sprite;
          var i;
          
          function createSpriteObjects() {
          
              for (var i=0; i<parseInt(textField.text); i++){
              component = Qt.createComponent("Sprite.qml");
              sprite = component.createObject(appWindow, {"x": 400, "y": i*100});
              }
          
              if (sprite === null) {
                  // Error Handling
                  console.log("Error creating object");
              }
          }
          

          Is there any way to do that instead of defining another button and sending signal to destroy function. Thanks in advance.

          DiracsbracketD Offline
          DiracsbracketD Offline
          Diracsbracket
          wrote on last edited by Diracsbracket
          #4

          @fgfgfg
          You need to keep track of the object you create, and when you no longer need them, i.e. just before creating your new batch of objects, you should destroy them.
          http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html#deleting-objects-dynamically

          As to the question of how you keep track of those objects, you could e.g. store the objects in a list, or iterate over the children of the common parent object to find the objects and then invoke the destroy() method on each of these.

          Good luck!

          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