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 | |
53 | static 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(0, name); |
61 | w->setMinimumSize(minimum); |
62 | w->setPreferredSize(preferred); |
63 | w->setMaximumSize(maximum); |
64 | |
65 | w->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); |
66 | return w; |
67 | } |
68 | |
69 | int main(int argc, char **argv) |
70 | { |
71 | QApplication app(argc, argv); |
72 | |
73 | QGraphicsScene scene; |
74 | scene.setSceneRect(0, 0, 800, 480); |
75 | |
76 | QSizeF minSize(30, 100); |
77 | QSizeF prefSize(210, 100); |
78 | QSizeF maxSize(300, 100); |
79 | |
80 | QGraphicsProxyWidget *a = createItem(minSize, prefSize, maxSize, "A" ); |
81 | QGraphicsProxyWidget *b = createItem(minSize, prefSize, maxSize, "B" ); |
82 | QGraphicsProxyWidget *c = createItem(minSize, prefSize, maxSize, "C" ); |
83 | QGraphicsProxyWidget *d = createItem(minSize, prefSize, maxSize, "D" ); |
84 | QGraphicsProxyWidget *e = createItem(minSize, prefSize, maxSize, "E" ); |
85 | QGraphicsProxyWidget *f = createItem(QSizeF(30, 50), QSizeF(150, 50), maxSize, "F (overflow)" ); |
86 | QGraphicsProxyWidget *g = createItem(QSizeF(30, 50), QSizeF(30, 100), maxSize, "G (overflow)" ); |
87 | |
88 | QGraphicsAnchorLayout *l = new QGraphicsAnchorLayout; |
89 | l->setSpacing(0); |
90 | |
91 | QGraphicsWidget *w = new QGraphicsWidget(nullptr, Qt::Window); |
92 | w->setPos(20, 20); |
93 | w->setLayout(l); |
94 | |
95 | // vertical |
96 | l->addAnchor(a, Qt::AnchorTop, l, Qt::AnchorTop); |
97 | l->addAnchor(b, Qt::AnchorTop, l, Qt::AnchorTop); |
98 | |
99 | l->addAnchor(c, Qt::AnchorTop, a, Qt::AnchorBottom); |
100 | l->addAnchor(c, Qt::AnchorTop, b, Qt::AnchorBottom); |
101 | l->addAnchor(c, Qt::AnchorBottom, d, Qt::AnchorTop); |
102 | l->addAnchor(c, Qt::AnchorBottom, e, Qt::AnchorTop); |
103 | |
104 | l->addAnchor(d, Qt::AnchorBottom, l, Qt::AnchorBottom); |
105 | l->addAnchor(e, Qt::AnchorBottom, l, Qt::AnchorBottom); |
106 | |
107 | l->addAnchor(c, Qt::AnchorTop, f, Qt::AnchorTop); |
108 | l->addAnchor(c, Qt::AnchorVerticalCenter, f, Qt::AnchorBottom); |
109 | l->addAnchor(f, Qt::AnchorBottom, g, Qt::AnchorTop); |
110 | l->addAnchor(c, Qt::AnchorBottom, g, Qt::AnchorBottom); |
111 | |
112 | // horizontal |
113 | l->addAnchor(l, Qt::AnchorLeft, a, Qt::AnchorLeft); |
114 | l->addAnchor(l, Qt::AnchorLeft, d, Qt::AnchorLeft); |
115 | l->addAnchor(a, Qt::AnchorRight, b, Qt::AnchorLeft); |
116 | |
117 | l->addAnchor(a, Qt::AnchorRight, c, Qt::AnchorLeft); |
118 | l->addAnchor(c, Qt::AnchorRight, e, Qt::AnchorLeft); |
119 | |
120 | l->addAnchor(b, Qt::AnchorRight, l, Qt::AnchorRight); |
121 | l->addAnchor(e, Qt::AnchorRight, l, Qt::AnchorRight); |
122 | l->addAnchor(d, Qt::AnchorRight, e, Qt::AnchorLeft); |
123 | |
124 | l->addAnchor(l, Qt::AnchorLeft, f, Qt::AnchorLeft); |
125 | l->addAnchor(l, Qt::AnchorLeft, g, Qt::AnchorLeft); |
126 | l->addAnchor(f, Qt::AnchorRight, g, Qt::AnchorRight); |
127 | |
128 | |
129 | scene.addItem(w); |
130 | scene.setBackgroundBrush(Qt::darkGreen); |
131 | QGraphicsView view(&scene); |
132 | |
133 | view.show(); |
134 | |
135 | return app.exec(); |
136 | } |
137 | |