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 class Widget : public QGraphicsWidget
54 {
55 public:
Widget(const QColor & color,const QColor & textColor,const QString & caption,QGraphicsItem * parent=nullptr)56     Widget(const QColor &color, const QColor &textColor, const QString &caption,
57            QGraphicsItem *parent = nullptr)
58         : QGraphicsWidget(parent)
59         , caption(caption)
60         , color(color)
61         , textColor(textColor)
62     {
63     }
64 
paint(QPainter * painter,const QStyleOptionGraphicsItem *,QWidget * =nullptr)65     void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * = nullptr) override
66     {
67         QFont font;
68         font.setPixelSize(0.75 * qMin(boundingRect().width(), boundingRect().height()));
69 
70         painter->fillRect(boundingRect(), color);
71         painter->save();
72         painter->setFont(font);
73         painter->setPen(textColor);
74         painter->drawText(boundingRect(), Qt::AlignCenter, caption);
75         painter->restore();
76     }
77 
78 private:
79     QString caption;
80     QColor color;
81     QColor textColor;
82 };
83 
main(int argc,char * argv[])84 int main(int argc, char *argv[])
85 {
86     QApplication app(argc, argv);
87 
88     QGraphicsScene scene;
89 
90     Widget *a = new Widget(Qt::blue, Qt::white, "a");
91     a->setPreferredSize(100, 100);
92     Widget *b = new Widget(Qt::green, Qt::black, "b");
93     b->setPreferredSize(100, 100);
94     Widget *c = new Widget(Qt::red, Qt::black, "c");
95     c->setPreferredSize(100, 100);
96 
97     QGraphicsAnchorLayout *layout = new QGraphicsAnchorLayout;
98 /*
99     //! [adding a corner anchor in two steps]
100     layout->addAnchor(a, Qt::AnchorTop, layout, Qt::AnchorTop);
101     layout->addAnchor(a, Qt::AnchorLeft, layout, Qt::AnchorLeft);
102     //! [adding a corner anchor in two steps]
103 */
104     //! [adding a corner anchor]
105     layout->addCornerAnchors(a, Qt::TopLeftCorner, layout, Qt::TopLeftCorner);
106     //! [adding a corner anchor]
107 
108     //! [adding anchors]
109     layout->addAnchor(b, Qt::AnchorLeft, a, Qt::AnchorRight);
110     layout->addAnchor(b, Qt::AnchorTop, a, Qt::AnchorBottom);
111     //! [adding anchors]
112 
113     // Place a third widget below the second.
114     layout->addAnchor(b, Qt::AnchorBottom, c, Qt::AnchorTop);
115 
116 /*
117     //! [adding anchors to match sizes in two steps]
118     layout->addAnchor(b, Qt::AnchorLeft, c, Qt::AnchorLeft);
119     layout->addAnchor(b, Qt::AnchorRight, c, Qt::AnchorRight);
120     //! [adding anchors to match sizes in two steps]
121 */
122 
123     //! [adding anchors to match sizes]
124     layout->addAnchors(b, c, Qt::Horizontal);
125     //! [adding anchors to match sizes]
126 
127     // Anchor the bottom-right corner of the third widget to the bottom-right
128     // corner of the layout.
129     layout->addCornerAnchors(c, Qt::BottomRightCorner, layout, Qt::BottomRightCorner);
130 
131     auto w = new QGraphicsWidget(nullptr, Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
132     w->setPos(20, 20);
133     w->setMinimumSize(100, 100);
134     w->setPreferredSize(320, 240);
135     w->setLayout(layout);
136     w->setWindowTitle(QApplication::translate("simpleanchorlayout", "QGraphicsAnchorLayout in use"));
137     scene.addItem(w);
138 
139     QGraphicsView view;
140     view.setScene(&scene);
141     view.setWindowTitle(QApplication::translate("simpleanchorlayout", "Simple Anchor Layout"));
142 
143     view.resize(360, 320);
144     view.show();
145 
146     return app.exec();
147 }
148