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. [SOLVED] create object dimanically problem
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] create object dimanically problem

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 1.2k 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.
  • G Offline
    G Offline
    gedixss
    wrote on last edited by
    #1

    I want to create dynamically qml objects, from another qml file with javascript. Problem: "that pressing a button on the newly created buttons rise to new text from Array (myCars[])".

    I want to do when i pressed the button named "Array" created new objects (buttons) has a different text from Array (MyCars[]).

    Thanks for any help. ;)

    These are files:

    My main.qml file:

    @import QtQuick 2.0
    import QtQuick.Controls 1.0
    import "componentCreation.js" as MyScript

    Rectangle {
    id: appWindow
    width: 300; height: 300

    Item {
        id: container
        width: 60; height: 400;
        anchors.horizontalCenter: parent.horizontalCenter;
    
        Text {
            id: spp;
        }
    }
    
    Button {
        text: "Array";
        opacity: 80;
        onClicked: {
            MyScript.createSpriteObjects();
        }
    }
    

    }@

    Sprite.qml (button created as object):

    @import QtQuick 2.0
    import "componentCreation.js" as MyScript

    Rectangle {
    id: button;
    anchors.horizontalCenter: parent.horizontalCenter;
    width: 50;
    height: 20;
    color: "green"

    MouseArea {
        anchors.fill: parent
        onClicked: {
            console.log("Clicked");
    
    
        }
    }
    

    }@

    Object create function "componentCreation.js":

    @function createSpriteObjects() {
    var component = Qt.createComponent("Sprite.qml");
    var myCars=new Array("A","B","C");
    var sprite;

    for (var i=0; i<myCars.length; i++) {
        sprite = component.createObject(container);
        sprite.y = (sprite.height + 15) * i;
        spp.text = myCars[i];
    }
    

    }@

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      So far i have understood from your explanation is you want to set text (viz. "A","B" or "C") from Array inside the Sprite.qml from the "componentCreation.js"

      If that is the case the you need to declare an alias in Sprite.qml and then you can access that alias from the js.

      So add this in Sprite.qml at the top
      @
      property alias customtext: button.mytext
      property string mytext
      @

      then add a Text element in the same as,
      @
      Text {
      id: txt
      text: mytext
      }
      @

      then access the "customtext" property from js as,
      @
      sprite.customtext = myCars[i];
      @

      Hope this helps a little.

      157

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gedixss
        wrote on last edited by
        #3

        It works, very thanks you for the reply. :)

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          You're welcome :) You can mark the thread as solved. Edit the title of this thread an prepend [solved].

          157

          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