1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include <QtWidgets>
52
53static QGraphicsProxyWidget *createItem(const QSizeF &minimum = QSizeF(100.0, 100.0),
54 const QSizeF &preferred = QSize(150.0, 100.0),
55 const QSizeF &maximum = QSizeF(200.0, 100.0),
56 const QString &name = "0")
57{
58 QGraphicsProxyWidget *w = new QGraphicsProxyWidget;
59 w->setWidget(new QPushButton(name));
60 w->setData(key: 0, value: name);
61 w->setMinimumSize(minimum);
62 w->setPreferredSize(preferred);
63 w->setMaximumSize(maximum);
64
65 w->setSizePolicy(hPolicy: QSizePolicy::Preferred, vPolicy: QSizePolicy::Preferred);
66 return w;
67}
68
69int main(int argc, char **argv)
70{
71 QApplication app(argc, argv);
72
73 QGraphicsScene scene;
74 scene.setSceneRect(x: 0, y: 0, w: 800, h: 480);
75
76 QSizeF minSize(30, 100);
77 QSizeF prefSize(210, 100);
78 QSizeF maxSize(300, 100);
79
80 QGraphicsProxyWidget *a = createItem(minimum: minSize, preferred: prefSize, maximum: maxSize, name: "A");
81 QGraphicsProxyWidget *b = createItem(minimum: minSize, preferred: prefSize, maximum: maxSize, name: "B");
82 QGraphicsProxyWidget *c = createItem(minimum: minSize, preferred: prefSize, maximum: maxSize, name: "C");
83 QGraphicsProxyWidget *d = createItem(minimum: minSize, preferred: prefSize, maximum: maxSize, name: "D");
84 QGraphicsProxyWidget *e = createItem(minimum: minSize, preferred: prefSize, maximum: maxSize, name: "E");
85 QGraphicsProxyWidget *f = createItem(minimum: QSizeF(30, 50), preferred: QSizeF(150, 50), maximum: maxSize, name: "F (overflow)");
86 QGraphicsProxyWidget *g = createItem(minimum: QSizeF(30, 50), preferred: QSizeF(30, 100), maximum: maxSize, name: "G (overflow)");
87
88 QGraphicsAnchorLayout *l = new QGraphicsAnchorLayout;
89 l->setSpacing(0);
90
91 QGraphicsWidget *w = new QGraphicsWidget(nullptr, Qt::Window);
92 w->setPos(ax: 20, ay: 20);
93 w->setLayout(l);
94
95 // vertical
96 l->addAnchor(firstItem: a, firstEdge: Qt::AnchorTop, secondItem: l, secondEdge: Qt::AnchorTop);
97 l->addAnchor(firstItem: b, firstEdge: Qt::AnchorTop, secondItem: l, secondEdge: Qt::AnchorTop);
98
99 l->addAnchor(firstItem: c, firstEdge: Qt::AnchorTop, secondItem: a, secondEdge: Qt::AnchorBottom);
100 l->addAnchor(firstItem: c, firstEdge: Qt::AnchorTop, secondItem: b, secondEdge: Qt::AnchorBottom);
101 l->addAnchor(firstItem: c, firstEdge: Qt::AnchorBottom, secondItem: d, secondEdge: Qt::AnchorTop);
102 l->addAnchor(firstItem: c, firstEdge: Qt::AnchorBottom, secondItem: e, secondEdge: Qt::AnchorTop);
103
104 l->addAnchor(firstItem: d, firstEdge: Qt::AnchorBottom, secondItem: l, secondEdge: Qt::AnchorBottom);
105 l->addAnchor(firstItem: e, firstEdge: Qt::AnchorBottom, secondItem: l, secondEdge: Qt::AnchorBottom);
106
107 l->addAnchor(firstItem: c, firstEdge: Qt::AnchorTop, secondItem: f, secondEdge: Qt::AnchorTop);
108 l->addAnchor(firstItem: c, firstEdge: Qt::AnchorVerticalCenter, secondItem: f, secondEdge: Qt::AnchorBottom);
109 l->addAnchor(firstItem: f, firstEdge: Qt::AnchorBottom, secondItem: g, secondEdge: Qt::AnchorTop);
110 l->addAnchor(firstItem: c, firstEdge: Qt::AnchorBottom, secondItem: g, secondEdge: Qt::AnchorBottom);
111
112 // horizontal
113 l->addAnchor(firstItem: l, firstEdge: Qt::AnchorLeft, secondItem: a, secondEdge: Qt::AnchorLeft);
114 l->addAnchor(firstItem: l, firstEdge: Qt::AnchorLeft, secondItem: d, secondEdge: Qt::AnchorLeft);
115 l->addAnchor(firstItem: a, firstEdge: Qt::AnchorRight, secondItem: b, secondEdge: Qt::AnchorLeft);
116
117 l->addAnchor(firstItem: a, firstEdge: Qt::AnchorRight, secondItem: c, secondEdge: Qt::AnchorLeft);
118 l->addAnchor(firstItem: c, firstEdge: Qt::AnchorRight, secondItem: e, secondEdge: Qt::AnchorLeft);
119
120 l->addAnchor(firstItem: b, firstEdge: Qt::AnchorRight, secondItem: l, secondEdge: Qt::AnchorRight);
121 l->addAnchor(firstItem: e, firstEdge: Qt::AnchorRight, secondItem: l, secondEdge: Qt::AnchorRight);
122 l->addAnchor(firstItem: d, firstEdge: Qt::AnchorRight, secondItem: e, secondEdge: Qt::AnchorLeft);
123
124 l->addAnchor(firstItem: l, firstEdge: Qt::AnchorLeft, secondItem: f, secondEdge: Qt::AnchorLeft);
125 l->addAnchor(firstItem: l, firstEdge: Qt::AnchorLeft, secondItem: g, secondEdge: Qt::AnchorLeft);
126 l->addAnchor(firstItem: f, firstEdge: Qt::AnchorRight, secondItem: g, secondEdge: Qt::AnchorRight);
127
128
129 scene.addItem(item: w);
130 scene.setBackgroundBrush(Qt::darkGreen);
131 QGraphicsView view(&scene);
132
133 view.show();
134
135 return app.exec();
136}
137

source code of qtbase/examples/widgets/graphicsview/anchorlayout/main.cpp