diff options
Diffstat (limited to 'examples/declarative/cppextensions/qgraphicslayouts/qgraphicslinearlayout')
3 files changed, 32 insertions, 11 deletions
diff --git a/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslinearlayout/linearlayout.cpp b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslinearlayout/linearlayout.cpp index 5cd35cb..080b6ce 100644 --- a/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslinearlayout/linearlayout.cpp +++ b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslinearlayout/linearlayout.cpp @@ -44,7 +44,7 @@ #include <QGraphicsLayoutItem> LinearLayoutAttached::LinearLayoutAttached(QObject *parent) -: QObject(parent), m_stretch(1), m_alignment(Qt::AlignCenter), m_spacing(0) +: QObject(parent), m_stretch(1), m_alignment(Qt::AlignTop), m_spacing(0) { } @@ -98,7 +98,6 @@ void QGraphicsLinearLayoutObject::insertLayoutItem(int index, QGraphicsLayoutIte { insertItem(index, item); - //connect attached properties if (LinearLayoutAttached *obj = attachedProperties.value(item)) { setStretchFactor(item, obj->stretchFactor()); setAlignment(item, obj->alignment()); @@ -109,12 +108,23 @@ void QGraphicsLinearLayoutObject::insertLayoutItem(int index, QGraphicsLayoutIte this, SLOT(updateAlignment(QGraphicsLayoutItem*,Qt::Alignment))); QObject::connect(obj, SIGNAL(spacingChanged(QGraphicsLayoutItem*,int)), this, SLOT(updateSpacing(QGraphicsLayoutItem*,int))); - //### need to disconnect when widget is removed? } } +void QGraphicsLinearLayoutObject::removeAt(int index) +{ + QGraphicsLayoutItem *item = itemAt(index); + if (item) { + LinearLayoutAttached *obj = attachedProperties.value(item); + obj->disconnect(this); + attachedProperties.remove(item); + } + QGraphicsLinearLayout::removeAt(index); +} + void QGraphicsLinearLayoutObject::clearChildren() { + // do not delete the removed items; they will be deleted by the QML engine while (count() > 0) removeAt(count()-1); } @@ -156,11 +166,9 @@ void QGraphicsLinearLayoutObject::updateAlignment(QGraphicsLayoutItem *item, Qt: QHash<QGraphicsLayoutItem*, LinearLayoutAttached*> QGraphicsLinearLayoutObject::attachedProperties; LinearLayoutAttached *QGraphicsLinearLayoutObject::qmlAttachedProperties(QObject *obj) { - // ### This is not allowed - you must attach to any object - if (!qobject_cast<QGraphicsLayoutItem*>(obj)) - return 0; LinearLayoutAttached *rv = new LinearLayoutAttached(obj); - attachedProperties.insert(qobject_cast<QGraphicsLayoutItem*>(obj), rv); + if (qobject_cast<QGraphicsLayoutItem*>(obj)) + attachedProperties.insert(qobject_cast<QGraphicsLayoutItem*>(obj), rv); return rv; } diff --git a/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslinearlayout/linearlayout.h b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslinearlayout/linearlayout.h index 392f3f8..e6daf4d 100644 --- a/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslinearlayout/linearlayout.h +++ b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslinearlayout/linearlayout.h @@ -74,11 +74,13 @@ public: QDeclarativeListProperty<QGraphicsLayoutItem> children() { return QDeclarativeListProperty<QGraphicsLayoutItem>(this, 0, children_append, children_count, children_at, children_clear); } - static LinearLayoutAttached *qmlAttachedProperties(QObject *); - qreal contentsMargin() const; void setContentsMargin(qreal); + void removeAt(int index); + + static LinearLayoutAttached *qmlAttachedProperties(QObject *); + private slots: void updateStretch(QGraphicsLayoutItem*,int); void updateSpacing(QGraphicsLayoutItem*,int); diff --git a/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslinearlayout/qgraphicslinearlayout.qml b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslinearlayout/qgraphicslinearlayout.qml index da77921..72cdeb6 100644 --- a/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslinearlayout/qgraphicslinearlayout.qml +++ b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslinearlayout/qgraphicslinearlayout.qml @@ -49,17 +49,28 @@ Item { size.width: parent.width size.height: parent.height + /* + Below we create a linear layout using the GraphicsLinearLayout item + (defined by the GraphicsLinearLayoutObject class in linearlayout.h). + + The first LayoutItem uses 'GraphicsLinearLayout.spacing' to set the + item's spacing: this is an attached property, set using the + properties defined in the LinearLayoutAttached class (also defined + in linearlayout.h). + */ + layout: QGraphicsLinearLayout { LayoutItem { + QGraphicsLinearLayout.spacing: 50 minimumSize: "100x100" - maximumSize: "300x300" + maximumSize: "200x200" preferredSize: "100x100" Rectangle { color: "yellow"; anchors.fill: parent } } LayoutItem { minimumSize: "100x100" maximumSize: "400x400" - preferredSize: "200x200" + preferredSize: "300x300" Rectangle { color: "green"; anchors.fill: parent } } } |