0

I'm using Javascript to dynamically create a QML component via:

var component = Qt.createComponent(qmlURL)
var item = component.createObject(parentLayout, properties)

In this case, the parentLayout is a ColumnLayout and it always creates the object at the end of the children list (which means it is displayed at the bottom of the column).

How can I specify the index position within the children of where the object is placed, or how can I move it in the children list post instantiation?

(I tried assigning parentLayout.children[i] to no avail)

Use case: I have a large existing multi-level (nested) Menu system that is static, but I'd like insert and remove custom submenu items in the hierarchy in response to external plugins registering/unregistering via a service API. While we could completely re-implement the menu system as a dynamic treeview, that seems overkill just to insert a couple menu subitems dynamically.

DavidJ
  • 4,369
  • 4
  • 26
  • 42
  • What is your main goal? I mean what a sense to specify the index instead of creating items in natural order? – folibis Oct 23 '18 at 18:34
  • Why not use `ListView` and a model which is more flexible . – Redanium Oct 23 '18 at 18:47
  • @Redanium probably because it doesn't offer custom layouting – dtech Oct 23 '18 at 19:20
  • @folibis obviously if he wanted to create things in order he wouldn't need to specify an index in the first place. Not all final intent can be specified sequentially. Just imagine not being able to move the cursor and simply insert a word in an already written sentence... This is actually quite an omission in qt's layouts. – dtech Oct 23 '18 at 19:24
  • adding a minimal working example that shows how this is happening will be much more helpful. – bardao Oct 24 '18 at 00:58
  • Just delete all of the items that come after the new index without deleting the item you wish to move, and re-create the deleted items.. voila.. simple as that – mike510a Oct 26 '18 at 02:06

0 Answers0