summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan-Arve Sæther <jan-arve.saether@nokia.com>2009-03-31 15:03:06 (GMT)
committerJan-Arve Sæther <jan-arve.saether@nokia.com>2009-03-31 15:05:19 (GMT)
commit981be3911e925c3f501cc85cdecf9dc3f2a73f43 (patch)
tree3a436226390de39bd49c86f54c6f0f9e234a0603
parent22500faf878063df5fbd0392aaa876340e9f9c77 (diff)
downloadQt-981be3911e925c3f501cc85cdecf9dc3f2a73f43.zip
Qt-981be3911e925c3f501cc85cdecf9dc3f2a73f43.tar.gz
Qt-981be3911e925c3f501cc85cdecf9dc3f2a73f43.tar.bz2
Improve example so that we can demonstrate custom layout items.
Should show better how to reimplement setGeometry(), sizeHint()
-rw-r--r--doc/src/examples/basicgraphicslayouts.qdoc55
-rw-r--r--examples/graphicsview/basicgraphicslayouts/layoutitem.cpp50
-rw-r--r--examples/graphicsview/basicgraphicslayouts/layoutitem.h12
3 files changed, 90 insertions, 27 deletions
diff --git a/doc/src/examples/basicgraphicslayouts.qdoc b/doc/src/examples/basicgraphicslayouts.qdoc
index 92571af..9696fb6 100644
--- a/doc/src/examples/basicgraphicslayouts.qdoc
+++ b/doc/src/examples/basicgraphicslayouts.qdoc
@@ -45,6 +45,7 @@
The Basic Graphics Layouts example shows how to use the layout classes
in QGraphicsView: QGraphicsLinearLayout and QGraphicsGridLayout.
+ In addition to that it shows how to write your own custom layout item.
\image basicgraphicslayouts-example.png Screenshot of the Basic Layouts Example
@@ -115,26 +116,24 @@
\section1 LayoutItem Class Definition
- The \c LayoutItem class is a subclass of QGraphicsWidget. It has a
- constructor, a destructor, and a reimplementation of the
- {QGraphicsItem::paint()}{paint()} function.
+ The \c LayoutItem class is a subclass of QGraphicsLayoutItem and
+ QGraphicsItem. It has a constructor, a destructor, and some required
+ reimplementations.
+ Since it inherits QGraphicsLayoutItem it must reimplement
+ {QGraphicsLayoutItem::setGeometry()}{setGeometry()} and
+ {QGraphicsLayoutItem::sizeHint()}{sizeHint()}.
+ In addition to that it inherits QGraphicsItem, so it must reimplement
+ {QGraphicsItem::boundingRect()}{boundingRect()} and
+ {QGraphicsItem::paint()}{paint()}.
\snippet examples/graphicsview/basicgraphicslayouts/layoutitem.h 0
- The \c LayoutItem class also has a private instance of QPixmap, \c pix.
-
- \note We subclass QGraphicsWidget so that \c LayoutItem objects can
- be automatically plugged into a layout, as QGraphicsWidget is a
- specialization of QGraphicsLayoutItem.
+ The \c LayoutItem class also has a private instance of QPixmap, \c m_pix.
\section1 LayoutItem Class Implementation
- In \c{LayoutItem}'s constructor, \c pix is instantiated and the
- \c{QT_original_R.png} image is loaded into it. We set the size of
- \c LayoutItem to be slightly larger than the size of the pixmap as we
- require some space around it for borders that we will paint later.
- Alternatively, you could scale the pixmap to prevent the item from
- becoming smaller than the pixmap.
+ In \c{LayoutItem}'s constructor, \c m_pix is instantiated and the
+ \c{block.png} image is loaded into it.
\snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 0
@@ -148,4 +147,32 @@
\snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 2
+ The reimplementation of {QGraphicsItem::boundingRect()}{boundingRect()}
+ will set the top left corner at (0,0), and the size of it will be
+ the size of the layout items
+ {QGraphicsLayoutItem::geometry()}{geometry()}. This is the area that
+ we paint within.
+
+ \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 3
+
+
+ The reimplementation of {QGraphicsLayoutItem::setGeometry()}{setGeometry()}
+ simply calls its baseclass implementation. However, since this will change
+ the boundingRect we must also call
+ {QGraphicsItem::prepareGeometryChange()}{prepareGeometryChange()}.
+ Finally, we move the item according to \c geom.topLeft().
+
+ \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 4
+
+
+ Since we don't want the size of the item to be smaller than the pixmap, we
+ must make sure that we return a size hint that is larger than \c m_pix.
+ We also add some extra space around for borders that we will paint later.
+ Alternatively, you could scale the pixmap to prevent the item from
+ becoming smaller than the pixmap.
+ The preferred size is the same as the minimum size hint, while we set
+ maximum to be a large value
+
+ \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 5
+
*/ \ No newline at end of file
diff --git a/examples/graphicsview/basicgraphicslayouts/layoutitem.cpp b/examples/graphicsview/basicgraphicslayouts/layoutitem.cpp
index 8216b6e..7311b65 100644
--- a/examples/graphicsview/basicgraphicslayouts/layoutitem.cpp
+++ b/examples/graphicsview/basicgraphicslayouts/layoutitem.cpp
@@ -43,19 +43,18 @@
//! [0]
LayoutItem::LayoutItem(QGraphicsItem *parent/* = 0*/)
- : QGraphicsWidget(parent)
+ : QGraphicsLayoutItem(), QGraphicsItem(parent)
{
- pix = new QPixmap(QLatin1String(":/images/block.png"));
- // Do not allow a size smaller than the pixmap with two frames around it.
- setMinimumSize(pix->size() + QSize(12, 12));
+ m_pix = new QPixmap(QLatin1String(":/images/block.png"));
+ setGraphicsItem(this);
}
//! [0]
LayoutItem::~LayoutItem()
{
- delete pix;
+ delete m_pix;
}
-
+
//! [1]
void LayoutItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget /*= 0*/)
@@ -64,8 +63,8 @@ void LayoutItem::paint(QPainter *painter,
Q_UNUSED(option);
QRectF frame(QPointF(0,0), geometry().size());
- qreal w = pix->width();
- qreal h = pix->height();
+ qreal w = m_pix->width();
+ qreal h = m_pix->height();
QGradientStops stops;
//! [1]
@@ -94,6 +93,39 @@ void LayoutItem::paint(QPainter *painter,
gradient.setStops(stops);
painter->setBrush(QBrush(gradient));
painter->drawRoundedRect(innerFrame, 10.0, 10.0);
- painter->drawPixmap(pixpos, *pix);
+ painter->drawPixmap(pixpos, *m_pix);
}
//! [2]
+
+//! [3]
+QRectF LayoutItem::boundingRect() const
+{
+ return QRectF(QPointF(0,0), geometry().size());
+}
+//! [3]
+
+//! [4]
+void LayoutItem::setGeometry(const QRectF &geom)
+{
+ prepareGeometryChange();
+ QGraphicsLayoutItem::setGeometry(geom);
+ setPos(geom.topLeft());
+}
+//! [4]
+
+//! [5]
+QSizeF LayoutItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
+{
+ switch (which) {
+ case Qt::MinimumSize:
+ case Qt::PreferredSize:
+ // Do not allow a size smaller than the pixmap with two frames around it.
+ return m_pix->size() + QSize(12, 12);
+ case Qt::MaximumSize:
+ return QSizeF(1000,1000);
+ default:
+ break;
+ }
+ return constraint;
+}
+//! [5]
diff --git a/examples/graphicsview/basicgraphicslayouts/layoutitem.h b/examples/graphicsview/basicgraphicslayouts/layoutitem.h
index cbda636..8ec9038 100644
--- a/examples/graphicsview/basicgraphicslayouts/layoutitem.h
+++ b/examples/graphicsview/basicgraphicslayouts/layoutitem.h
@@ -44,18 +44,22 @@
#include <QtGui>
//! [0]
-class LayoutItem : public QGraphicsWidget
+class LayoutItem : public QGraphicsLayoutItem, public QGraphicsItem
{
- Q_OBJECT
-
public:
LayoutItem(QGraphicsItem *parent = 0);
~LayoutItem();
+ // Inherited from QGraphicsLayoutItem
+ void setGeometry(const QRectF &geom);
+ QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
+
+ // Inherited from QGraphicsItem
+ QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget = 0);
private:
- QPixmap *pix;
+ QPixmap *m_pix;
};
//! [0]