Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qml: Delegate/Model component for floating model items

Tags:

c++

qt

qml

qtquick2

I have a dynamic set of QML components (these are based on/combine different controls like images, labels, ...) which are displayed on "arbitrary" positions within a parent control. The position of each component is defined by an underlying object (C++). At the moment I create and remove these components using dynamic object creation, everytime a new underlying object is created or removed.

Although this works it would be much cleaner to use a delegate/model scheme with an underlying QAbstractItemModel. Is there a built in component for this, e.g. a component which allows free positioning of the QAbstractItemModel's items?

[EDIT]: Here is a depiction of what I mean:

enter image description here

Regards,

like image 779
Hyndrix Avatar asked Dec 05 '25 09:12

Hyndrix


1 Answers

You can use a Repeater, which is typically used with a row or a column to lay things out, but it will also work for standalone items.

In addition to that you also have signals for added and removed items.

  Repeater {
    model: 20
    delegate: Rectangle {
      width: 50
      height: 50
      color: Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
      MouseArea {
        anchors.fill: parent
        onPositionChanged: {
          parent.x += mouseX
          parent.y += mouseY
        }
      }
    }
  }
like image 133
dtech Avatar answered Dec 06 '25 22:12

dtech