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. how to add an element to a layout at a given index?
Forum Updated to NodeBB v4.3 + New Features

how to add an element to a layout at a given index?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 348 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.
  • T Offline
    T Offline
    tubbadu
    wrote on last edited by
    #1

    Hello, I have a ColumnLayout and I'm generating dinamically objects:

    var newBlock = Qt.createComponent("Block.qml").createObject(document);
    

    where document is the id of the layout.
    this however creates a new object as last element of the columnlayout, but I need to create it at a given index. So I tried to move it like this:

    function arraymove(arr, fromIndex, toIndex) {
    		var element = arr[fromIndex];
    		arr.splice(fromIndex, 1);
    		arr.splice(toIndex, 0, element);
    	}
    ...
    var block = Qt.createQmlObject("import QtQuick 2.15; Block {}", document);
    arraymove(document.children, document.children.length-1, index);
    

    but I get this error: TypeError: Property 'splice' of object [object Object] is not a function and indeed typeof document.children returns object instead of a list of Item as the docs suggest. what is the problem?

    thanks in advance!

    GrecKoG 1 Reply Last reply
    0
    • T tubbadu

      Hello, I have a ColumnLayout and I'm generating dinamically objects:

      var newBlock = Qt.createComponent("Block.qml").createObject(document);
      

      where document is the id of the layout.
      this however creates a new object as last element of the columnlayout, but I need to create it at a given index. So I tried to move it like this:

      function arraymove(arr, fromIndex, toIndex) {
      		var element = arr[fromIndex];
      		arr.splice(fromIndex, 1);
      		arr.splice(toIndex, 0, element);
      	}
      ...
      var block = Qt.createQmlObject("import QtQuick 2.15; Block {}", document);
      arraymove(document.children, document.children.length-1, index);
      

      but I get this error: TypeError: Property 'splice' of object [object Object] is not a function and indeed typeof document.children returns object instead of a list of Item as the docs suggest. what is the problem?

      thanks in advance!

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      @tubbadu I would advise you to use model and views (ListView/Repeater) and don't instantiate your graphical components manually.

      T 1 Reply Last reply
      0
      • GrecKoG GrecKo

        @tubbadu I would advise you to use model and views (ListView/Repeater) and don't instantiate your graphical components manually.

        T Offline
        T Offline
        tubbadu
        wrote on last edited by
        #3

        @GrecKo thanks for the reply! What are the main advantages of using repeaters?

        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