QtBase  v6.3.1
main.cpp
Go to the documentation of this file.
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:
56  Widget(const QColor &color, const QColor &textColor, const QString &caption,
57  QGraphicsItem *parent = nullptr)
59  , caption(caption)
60  , color(color)
61  , textColor(textColor)
62  {
63  }
64 
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 
71  painter->save();
73  painter->setPen(textColor);
75  painter->restore();
76  }
77 
78 private:
79  QString caption;
80  QColor color;
81  QColor textColor;
82 };
83 
84 int main(int argc, char *argv[])
85 {
86  QApplication app(argc, argv);
87 
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 
98 /*
100  layout->addAnchor(a, Qt::AnchorTop, layout, Qt::AnchorTop);
101  layout->addAnchor(a, Qt::AnchorLeft, layout, Qt::AnchorLeft);
103 */
105  layout->addCornerAnchors(a, Qt::TopLeftCorner, layout, Qt::TopLeftCorner);
107 
109  layout->addAnchor(b, Qt::AnchorLeft, a, Qt::AnchorRight);
110  layout->addAnchor(b, Qt::AnchorTop, a, Qt::AnchorBottom);
112 
113  // Place a third widget below the second.
114  layout->addAnchor(b, Qt::AnchorBottom, c, Qt::AnchorTop);
115 
116 /*
118  layout->addAnchor(b, Qt::AnchorLeft, c, Qt::AnchorLeft);
119  layout->addAnchor(b, Qt::AnchorRight, c, Qt::AnchorRight);
121 */
122 
124  layout->addAnchors(b, c, Qt::Horizontal);
126 
127  // Anchor the bottom-right corner of the third widget to the bottom-right
128  // corner of the layout.
130 
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 
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 }
The QApplication class manages the GUI application's control flow and main settings.
Definition: qapplication.h:68
static int exec()
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
The QFont class specifies a query for a font used for drawing text.
Definition: qfont.h:56
void setPixelSize(int)
Definition: qfont.cpp:1069
The QGraphicsAnchorLayout class provides a layout where one can anchor widgets together in Graphics V...
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:83
QGraphicsObject * parent
the parent of the item
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.
void addItem(QGraphicsItem *item)
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
Definition: qgraphicsview.h:60
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.
QRectF boundingRect() const override
QFont font
the widgets' font
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
void setPen(const QColor &color)
Definition: qpainter.cpp:3640
void restore()
Definition: qpainter.cpp:1611
void save()
Definition: qpainter.cpp:1577
void setFont(const QFont &f)
Definition: qpainter.cpp:3866
void drawText(const QPointF &p, const QString &s)
Definition: qpainter.cpp:5444
void fillRect(const QRectF &, const QBrush &)
Definition: qpainter.cpp:6644
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem.
Definition: qstyleoption.h:684
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
int width
the width of the widget excluding any window frame
Definition: qwidget.h:148
int height
the height of the widget excluding any window frame
Definition: qwidget.h:149
Definition: widget.h:60
Widget(const QColor &color, const QColor &textColor, const QString &caption, QGraphicsItem *parent=nullptr)
Definition: main.cpp:56
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *=nullptr) override
Definition: main.cpp:65
int main(int argc, char **argv)
Definition: main.cpp:1
@ TopLeftCorner
Definition: qnamespace.h:1288
@ BottomRightCorner
Definition: qnamespace.h:1291
@ AlignCenter
Definition: qnamespace.h:188
@ Horizontal
Definition: qnamespace.h:124
@ white
Definition: qnamespace.h:62
@ blue
Definition: qnamespace.h:68
@ black
Definition: qnamespace.h:61
@ green
Definition: qnamespace.h:67
@ red
Definition: qnamespace.h:66
@ AnchorRight
Definition: qnamespace.h:1472
@ AnchorBottom
Definition: qnamespace.h:1475
@ AnchorTop
Definition: qnamespace.h:1473
@ AnchorLeft
Definition: qnamespace.h:1470
@ CustomizeWindowHint
Definition: qnamespace.h:264
@ Widget
Definition: qnamespace.h:231
@ Window
Definition: qnamespace.h:232
@ WindowTitleHint
Definition: qnamespace.h:251
GLboolean GLboolean GLboolean b
GLfloat GLfloat GLfloat w
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint color
[2]
const GLubyte * c
Definition: qopenglext.h:12701
QVBoxLayout * layout
QGraphicsScene scene
[0]
QApplication app(argc, argv)
[0]
QPainter painter(this)
[7]
QQuickView * view
[0]