|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 |
|
44 class Widget : public QGraphicsWidget |
|
45 { |
|
46 public: |
|
47 Widget(const QColor &color, const QColor &textColor, const QString &caption, |
|
48 QGraphicsItem *parent = 0) |
|
49 : QGraphicsWidget(parent), caption(caption), color(color), textColor(textColor) |
|
50 { |
|
51 } |
|
52 |
|
53 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) |
|
54 { |
|
55 QFont font; |
|
56 font.setPixelSize(0.75 * qMin(boundingRect().width(), boundingRect().height())); |
|
57 |
|
58 painter->fillRect(boundingRect(), color); |
|
59 painter->save(); |
|
60 painter->setFont(font); |
|
61 painter->setPen(textColor); |
|
62 painter->drawText(boundingRect(), Qt::AlignCenter, caption); |
|
63 painter->restore(); |
|
64 } |
|
65 |
|
66 private: |
|
67 QString caption; |
|
68 QColor color; |
|
69 QColor textColor; |
|
70 }; |
|
71 |
|
72 int main(int argc, char *argv[]) |
|
73 { |
|
74 QApplication app(argc, argv); |
|
75 |
|
76 QGraphicsScene *scene = new QGraphicsScene(); |
|
77 |
|
78 Widget *a = new Widget(Qt::blue, Qt::white, "a"); |
|
79 a->setPreferredSize(100, 100); |
|
80 Widget *b = new Widget(Qt::green, Qt::black, "b"); |
|
81 b->setPreferredSize(100, 100); |
|
82 Widget *c = new Widget(Qt::red, Qt::black, "c"); |
|
83 c->setPreferredSize(100, 100); |
|
84 |
|
85 QGraphicsAnchorLayout *layout = new QGraphicsAnchorLayout(); |
|
86 /* |
|
87 //! [adding a corner anchor in two steps] |
|
88 layout->addAnchor(a, Qt::AnchorTop, layout, Qt::AnchorTop); |
|
89 layout->addAnchor(a, Qt::AnchorLeft, layout, Qt::AnchorLeft); |
|
90 //! [adding a corner anchor in two steps] |
|
91 */ |
|
92 //! [adding a corner anchor] |
|
93 layout->addCornerAnchors(a, Qt::TopLeftCorner, layout, Qt::TopLeftCorner); |
|
94 //! [adding a corner anchor] |
|
95 |
|
96 //! [adding anchors] |
|
97 layout->addAnchor(b, Qt::AnchorLeft, a, Qt::AnchorRight); |
|
98 layout->addAnchor(b, Qt::AnchorTop, a, Qt::AnchorBottom); |
|
99 //! [adding anchors] |
|
100 |
|
101 // Place a third widget below the second. |
|
102 layout->addAnchor(b, Qt::AnchorBottom, c, Qt::AnchorTop); |
|
103 |
|
104 /* |
|
105 //! [adding anchors to match sizes in two steps] |
|
106 layout->addAnchor(b, Qt::AnchorLeft, c, Qt::AnchorLeft); |
|
107 layout->addAnchor(b, Qt::AnchorRight, c, Qt::AnchorRight); |
|
108 //! [adding anchors to match sizes in two steps] |
|
109 */ |
|
110 |
|
111 //! [adding anchors to match sizes] |
|
112 layout->addAnchors(b, c, Qt::Horizontal); |
|
113 //! [adding anchors to match sizes] |
|
114 |
|
115 // Anchor the bottom-right corner of the third widget to the bottom-right |
|
116 // corner of the layout. |
|
117 layout->addCornerAnchors(c, Qt::BottomRightCorner, layout, Qt::BottomRightCorner); |
|
118 |
|
119 QGraphicsWidget *w = new QGraphicsWidget(0, Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint); |
|
120 w->setPos(20, 20); |
|
121 w->setMinimumSize(100, 100); |
|
122 w->setPreferredSize(320, 240); |
|
123 w->setLayout(layout); |
|
124 w->setWindowTitle(QApplication::translate("simpleanchorlayout", "QGraphicsAnchorLayout in use")); |
|
125 scene->addItem(w); |
|
126 |
|
127 QGraphicsView *view = new QGraphicsView(); |
|
128 view->setScene(scene); |
|
129 view->setWindowTitle(QApplication::translate("simpleanchorlayout", "Simple Anchor Layout")); |
|
130 view->resize(360, 320); |
|
131 view->show(); |
|
132 |
|
133 return app.exec(); |
|
134 } |