source: trunk/examples/graphicsview/anchorlayout/main.cpp@ 1030

Last change on this file since 1030 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QGraphicsWidget>
42#include <QGraphicsProxyWidget>
43#include <QGraphicsAnchorLayout>
44#include <QtGui>
45
46static QGraphicsProxyWidget *createItem(const QSizeF &minimum = QSizeF(100.0, 100.0),
47 const QSizeF &preferred = QSize(150.0, 100.0),
48 const QSizeF &maximum = QSizeF(200.0, 100.0),
49 const QString &name = "0")
50{
51 QGraphicsProxyWidget *w = new QGraphicsProxyWidget;
52 w->setWidget(new QPushButton(name));
53 w->setData(0, name);
54 w->setMinimumSize(minimum);
55 w->setPreferredSize(preferred);
56 w->setMaximumSize(maximum);
57
58 w->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
59 return w;
60}
61
62int main(int argc, char **argv)
63{
64 QApplication app(argc, argv);
65
66 QGraphicsScene scene;
67 scene.setSceneRect(0, 0, 800, 480);
68
69 QSizeF minSize(30, 100);
70 QSizeF prefSize(210, 100);
71 QSizeF maxSize(300, 100);
72
73 QGraphicsProxyWidget *a = createItem(minSize, prefSize, maxSize, "A");
74 QGraphicsProxyWidget *b = createItem(minSize, prefSize, maxSize, "B");
75 QGraphicsProxyWidget *c = createItem(minSize, prefSize, maxSize, "C");
76 QGraphicsProxyWidget *d = createItem(minSize, prefSize, maxSize, "D");
77 QGraphicsProxyWidget *e = createItem(minSize, prefSize, maxSize, "E");
78 QGraphicsProxyWidget *f = createItem(QSizeF(30, 50), QSizeF(150, 50), maxSize, "F (overflow)");
79 QGraphicsProxyWidget *g = createItem(QSizeF(30, 50), QSizeF(30, 100), maxSize, "G (overflow)");
80
81 QGraphicsAnchorLayout *l = new QGraphicsAnchorLayout;
82 l->setSpacing(0);
83
84 QGraphicsWidget *w = new QGraphicsWidget(0, Qt::Window);
85 w->setPos(20, 20);
86 w->setLayout(l);
87
88 // vertical
89 QGraphicsAnchor *anchor = l->addAnchor(a, Qt::AnchorTop, l, Qt::AnchorTop);
90 anchor = l->addAnchor(b, Qt::AnchorTop, l, Qt::AnchorTop);
91
92 anchor = l->addAnchor(c, Qt::AnchorTop, a, Qt::AnchorBottom);
93 anchor = l->addAnchor(c, Qt::AnchorTop, b, Qt::AnchorBottom);
94 anchor = l->addAnchor(c, Qt::AnchorBottom, d, Qt::AnchorTop);
95 anchor = l->addAnchor(c, Qt::AnchorBottom, e, Qt::AnchorTop);
96
97 anchor = l->addAnchor(d, Qt::AnchorBottom, l, Qt::AnchorBottom);
98 anchor = l->addAnchor(e, Qt::AnchorBottom, l, Qt::AnchorBottom);
99
100 anchor = l->addAnchor(c, Qt::AnchorTop, f, Qt::AnchorTop);
101 anchor = l->addAnchor(c, Qt::AnchorVerticalCenter, f, Qt::AnchorBottom);
102 anchor = l->addAnchor(f, Qt::AnchorBottom, g, Qt::AnchorTop);
103 anchor = l->addAnchor(c, Qt::AnchorBottom, g, Qt::AnchorBottom);
104
105 // horizontal
106 anchor = l->addAnchor(l, Qt::AnchorLeft, a, Qt::AnchorLeft);
107 anchor = l->addAnchor(l, Qt::AnchorLeft, d, Qt::AnchorLeft);
108 anchor = l->addAnchor(a, Qt::AnchorRight, b, Qt::AnchorLeft);
109
110 anchor = l->addAnchor(a, Qt::AnchorRight, c, Qt::AnchorLeft);
111 anchor = l->addAnchor(c, Qt::AnchorRight, e, Qt::AnchorLeft);
112
113 anchor = l->addAnchor(b, Qt::AnchorRight, l, Qt::AnchorRight);
114 anchor = l->addAnchor(e, Qt::AnchorRight, l, Qt::AnchorRight);
115 anchor = l->addAnchor(d, Qt::AnchorRight, e, Qt::AnchorLeft);
116
117 anchor = l->addAnchor(l, Qt::AnchorLeft, f, Qt::AnchorLeft);
118 anchor = l->addAnchor(l, Qt::AnchorLeft, g, Qt::AnchorLeft);
119 anchor = l->addAnchor(f, Qt::AnchorRight, g, Qt::AnchorRight);
120
121
122 scene.addItem(w);
123 scene.setBackgroundBrush(Qt::darkGreen);
124 QGraphicsView view(&scene);
125 view.show();
126
127 return app.exec();
128}
Note: See TracBrowser for help on using the repository browser.