diff options
author | Bea Lam <bea.lam@nokia.com> | 2010-05-31 01:42:36 (GMT) |
---|---|---|
committer | Bea Lam <bea.lam@nokia.com> | 2010-05-31 01:52:57 (GMT) |
commit | 10dba5b02c6226bce186c0308d40fdedb4ecd9b8 (patch) | |
tree | 11cc47388e5df508a740fa81b052cdfe5b9e8184 /examples/declarative | |
parent | b64f49b7c97aa905cde93a943f56eca20475678e (diff) | |
download | Qt-10dba5b02c6226bce186c0308d40fdedb4ecd9b8.zip Qt-10dba5b02c6226bce186c0308d40fdedb4ecd9b8.tar.gz Qt-10dba5b02c6226bce186c0308d40fdedb4ecd9b8.tar.bz2 |
Implement removeAt() for layout classes to clean up item removal. Also
clean up qmlAttachedProperties() implementation and improve QML example
Diffstat (limited to 'examples/declarative')
8 files changed, 70 insertions, 36 deletions
diff --git a/examples/declarative/cppextensions/qgraphicslayouts/qgraphicsgridlayout/.main.cpp.swo b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicsgridlayout/.main.cpp.swo Binary files differindex c742274..cbd3b3e 100644 --- a/examples/declarative/cppextensions/qgraphicslayouts/qgraphicsgridlayout/.main.cpp.swo +++ b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicsgridlayout/.main.cpp.swo diff --git a/examples/declarative/cppextensions/qgraphicslayouts/qgraphicsgridlayout/gridlayout.cpp b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicsgridlayout/gridlayout.cpp index 728c225..1792696 100644 --- a/examples/declarative/cppextensions/qgraphicslayouts/qgraphicsgridlayout/gridlayout.cpp +++ b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicsgridlayout/gridlayout.cpp @@ -131,13 +131,23 @@ void QGraphicsGridLayoutObject::addLayoutItem(QGraphicsLayoutItem *item) setAlignment(item, alignment); QObject::connect(obj, SIGNAL(alignmentChanged(QGraphicsLayoutItem*, Qt::Alignment)), this, SLOT(updateAlignment(QGraphicsLayoutItem*, Qt::Alignment))); - //### need to disconnect when widget is removed? Re-implement removeAt()? } } +void QGraphicsGridLayoutObject::removeAt(int index) +{ + QGraphicsLayoutItem *item = itemAt(index); + if (item) { + GridLayoutAttached *obj = attachedProperties.value(item); + obj->disconnect(this); + attachedProperties.remove(item); + } + QGraphicsGridLayout::removeAt(index); +} + void QGraphicsGridLayoutObject::clearChildren() { - //### do I need to delete the removed items? And/or removed them from attachedProperties? + // do not delete the removed items; they will be deleted by the QML engine while (count() > 0) removeAt(count()-1); } @@ -146,7 +156,7 @@ qreal QGraphicsGridLayoutObject::spacing() const { if (verticalSpacing() == horizontalSpacing()) return verticalSpacing(); - return -1; //### + return -1; } qreal QGraphicsGridLayoutObject::contentsMargin() const @@ -170,11 +180,9 @@ void QGraphicsGridLayoutObject::updateAlignment(QGraphicsLayoutItem *item, Qt::A GridLayoutAttached *QGraphicsGridLayoutObject::qmlAttachedProperties(QObject *obj) { - // ### This is not allowed - you must attach to any object - if (!qobject_cast<QGraphicsLayoutItem*>(obj)) - return 0; GridLayoutAttached *rv = new GridLayoutAttached(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/qgraphicsgridlayout/gridlayout.h b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicsgridlayout/gridlayout.h index 04f0148..cb8d9b7 100644 --- a/examples/declarative/cppextensions/qgraphicslayouts/qgraphicsgridlayout/gridlayout.h +++ b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicsgridlayout/gridlayout.h @@ -69,6 +69,8 @@ public: qreal contentsMargin() const; void setContentsMargin(qreal); + void removeAt(int index); + static GridLayoutAttached *qmlAttachedProperties(QObject *); private slots: diff --git a/examples/declarative/cppextensions/qgraphicslayouts/qgraphicsgridlayout/qgraphicsgridlayout.qml b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicsgridlayout/qgraphicsgridlayout.qml index c3edf1d..5cb011f 100644 --- a/examples/declarative/cppextensions/qgraphicslayouts/qgraphicsgridlayout/qgraphicsgridlayout.qml +++ b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicsgridlayout/qgraphicsgridlayout.qml @@ -49,46 +49,48 @@ Item { size.width: parent.width size.height: parent.height + /* + Below we create a grid layout using the GraphicsGridLayout item + (defined by the GraphicsGridLayoutObject class in gridlayout.h). + + The row, column etc. are set through attached properties on + GraphicsGridLayout, using the properties defined in the + GridLayoutAttached class (also defined in gridlayout.h). + */ + layout: QGraphicsGridLayout { - objectName: "layout" LayoutItem { QGraphicsGridLayout.row: 0 QGraphicsGridLayout.column: 0 - QGraphicsGridLayout.alignment: Qt.AlignLeft minimumSize: "100x100" maximumSize: "300x300" - preferredSize: "100x100" + preferredSize: "200x200" Rectangle { color: "red"; anchors.fill: parent } } LayoutItem { - QGraphicsGridLayout.row: 1 - QGraphicsGridLayout.column: 0 + QGraphicsGridLayout.row: 0 + QGraphicsGridLayout.column: 1 minimumSize: "100x100" maximumSize: "200x200" - preferredSize: "100x100" + preferredSize: "200x200" Rectangle { color: "orange"; anchors.fill: parent } } LayoutItem { - QGraphicsGridLayout.row: 2 + QGraphicsGridLayout.row: 1 QGraphicsGridLayout.column: 0 + QGraphicsGridLayout.columnSpan: 2 minimumSize: "100x100" - maximumSize: "300x300" - preferredSize: "200x200" Rectangle { color: "yellow"; anchors.fill: parent } } LayoutItem { - QGraphicsGridLayout.row: 0 - QGraphicsGridLayout.column: 1 - minimumSize: "100x100" - maximumSize: "200x200" + QGraphicsGridLayout.row: 2 + QGraphicsGridLayout.column: 0 preferredSize: "200x200" Rectangle { color: "green"; anchors.fill: parent } } LayoutItem { - QGraphicsGridLayout.row: 1 + QGraphicsGridLayout.row: 2 QGraphicsGridLayout.column: 1 - minimumSize: "100x100" - maximumSize: "400x400" preferredSize: "200x200" Rectangle { color: "blue"; anchors.fill: parent } } diff --git a/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslayouts.pro b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslayouts.pro index d92a6f4..672120a 100644 --- a/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslayouts.pro +++ b/examples/declarative/cppextensions/qgraphicslayouts/qgraphicslayouts.pro @@ -1,5 +1,6 @@ TEMPLATE = subdirs SUBDIRS += \ - graphicsLayouts \ - layoutItem + layoutitem \ + qgraphicsgridlayout \ + qgraphicslinearlayout 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 } } } |