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 <QApplication>
52 #include <QLabel>
53 #include <QGraphicsAnchorLayout>
54 #include <QGraphicsProxyWidget>
55 #include <QGraphicsScene>
56 #include <QGraphicsSceneResizeEvent>
57 #include <QGraphicsView>
58 #include <QGraphicsWidget>
59 #include <QPainter>
60 #include <QPushButton>
61 
62 
63 class GraphicsView : public QGraphicsView
64 {
65 public:
68  {
69  }
70 
71  void resizeEvent(QResizeEvent *event) override
72  {
73  w->setGeometry(0, 0, event->size().width(), event->size().height());
74  }
75 
77 };
78 
80 {
81 public:
83  : QGraphicsLayoutItem(), original(new QGraphicsPixmapItem(pix))
84  , r(QRectF(QPointF(0, 0), pix.size()))
85  {
86  setGraphicsItem(original);
87  original->show();
88  }
89 
91  {
92  setGraphicsItem(nullptr);
93  delete original;
94  }
95 
97  {
98  original->setZValue(z);
99  }
100 
101  void setGeometry(const QRectF &rect) override
102  {
103  original->setTransform(QTransform::fromScale(rect.width() / r.width(),
104  rect.height() / r.height()), true);
105  original->setPos(rect.x(), rect.y());
106  r = rect;
107  }
108 
109 protected:
110  QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const override
111  {
112  Q_UNUSED(constraint);
113  QSizeF sh;
114  switch (which) {
115  case Qt::MinimumSize:
116  sh = QSizeF(0, 0);
117  break;
118  case Qt::PreferredSize:
119  sh = QSizeF(50, 50);
120  break;
121  case Qt::MaximumSize:
123  break;
124  default:
125  break;
126  }
127  return sh;
128  }
129 
130 private:
131  QGraphicsPixmapItem *original;
132  QRectF r;
133 };
134 
135 
137 {
138  Q_OBJECT
139 
140 public:
142  : QGraphicsWidget()
143  , original(pix)
144  , scaled(pix)
145  {
146  }
147 
149  {
150  const QPointF reflection(0, scaled.height() + 2);
151 
152  painter->drawPixmap(QPointF(), scaled);
153 
154  QPixmap tmp(scaled.size());
155  tmp.fill(Qt::transparent);
156  QPainter p(&tmp);
157 
158  // create gradient
159  QPoint p1(scaled.width() / 2, 0);
160  QPoint p2(scaled.width() / 2, scaled.height());
161  QLinearGradient linearGrad(p1, p2);
162  linearGrad.setColorAt(0, QColor(0, 0, 0, 0));
163  linearGrad.setColorAt(0.65, QColor(0, 0, 0, 127));
164  linearGrad.setColorAt(1, QColor(0, 0, 0, 255));
165 
166  // apply 'mask'
167  p.setBrush(linearGrad);
168  p.fillRect(0, 0, tmp.width(), tmp.height(), QBrush(linearGrad));
169  p.fillRect(0, 0, tmp.width(), tmp.height(), QBrush(linearGrad));
170 
171  // paint the image flipped
172  p.setCompositionMode(QPainter::CompositionMode_DestinationOver);
173  p.drawPixmap(0, 0, QPixmap::fromImage(scaled.toImage().mirrored(false, true)));
174  p.end();
175 
176  painter->drawPixmap(reflection, tmp);
177  }
178 
180  {
181  QSize newSize = event->newSize().toSize();
182  newSize.setHeight(newSize.height() / 2);
183  scaled = original.scaled(newSize);
184  }
185 
186  QRectF boundingRect() const override
187  {
188  QSize size(scaled.width(), scaled.height() * 2 + 2);
189  return QRectF(QPointF(0, 0), size);
190  }
191 
192 private:
193  QPixmap original;
194  QPixmap scaled;
195 };
196 
197 
198 int main(int argc, char *argv[])
199 {
200  Q_INIT_RESOURCE(weatheranchorlayout);
201 
202  QApplication app(argc, argv);
203 
205  scene.setSceneRect(0, 0, 800, 480);
206 
207  // pixmaps widgets
208  PixmapWidget *title = new PixmapWidget(QPixmap(":/images/title.jpg"));
209  PlaceWidget *place = new PlaceWidget(QPixmap(":/images/place.jpg"));
210  PixmapWidget *details = new PixmapWidget(QPixmap(":/images/5days.jpg"));
211  PixmapWidget *sunnyWeather = new PixmapWidget(QPixmap(":/images/weather-few-clouds.png"));
212  PixmapWidget *tabbar = new PixmapWidget(QPixmap(":/images/tabbar.jpg"));
213 
214 
215  // setup sizes
216  title->setPreferredSize(QSizeF(348, 45));
218 
219  place->setPreferredSize(QSizeF(96, 72));
221 
222  details->setMinimumSize(QSizeF(200, 112));
223  details->setPreferredSize(QSizeF(200, 112));
225 
226  tabbar->setPreferredSize(QSizeF(70, 24));
228 
229  sunnyWeather->setPreferredSize(QSizeF(128, 97));
231  sunnyWeather->setZValue(9999);
232 
233  // start anchor layout
235  layout->setSpacing(0);
236 
237  // setup the main widget
239  QPalette p;
240  p.setColor(QPalette::Window, Qt::black);
241  widget->setPalette(p);
242  widget->setPos(20, 20);
244 
245  // vertical anchors
247  anchor = layout->addAnchor(place, Qt::AnchorTop, title, Qt::AnchorBottom);
248  anchor->setSpacing(12);
249  anchor = layout->addAnchor(place, Qt::AnchorBottom, layout, Qt::AnchorBottom);
250  anchor->setSpacing(12);
251 
252  anchor = layout->addAnchor(sunnyWeather, Qt::AnchorTop, title, Qt::AnchorTop);
253  anchor = layout->addAnchor(sunnyWeather, Qt::AnchorBottom, layout, Qt::AnchorVerticalCenter);
254 
255  anchor = layout->addAnchor(tabbar, Qt::AnchorTop, title, Qt::AnchorBottom);
256  anchor->setSpacing(5);
257  anchor = layout->addAnchor(details, Qt::AnchorTop, tabbar, Qt::AnchorBottom);
258  anchor->setSpacing(2);
259  anchor = layout->addAnchor(details, Qt::AnchorBottom, layout, Qt::AnchorBottom);
260  anchor->setSpacing(12);
261 
262  // horizontal anchors
263  anchor = layout->addAnchor(layout, Qt::AnchorLeft, title, Qt::AnchorLeft);
264  anchor = layout->addAnchor(title, Qt::AnchorRight, layout, Qt::AnchorRight);
265 
266  anchor = layout->addAnchor(place, Qt::AnchorLeft, layout, Qt::AnchorLeft);
267  anchor->setSpacing(15);
268  anchor = layout->addAnchor(place, Qt::AnchorRight, details, Qt::AnchorLeft);
269  anchor->setSpacing(35);
270 
271  anchor = layout->addAnchor(sunnyWeather, Qt::AnchorLeft, place, Qt::AnchorHorizontalCenter);
272  anchor = layout->addAnchor(sunnyWeather, Qt::AnchorRight, layout, Qt::AnchorHorizontalCenter);
273 
274  anchor = layout->addAnchor(tabbar, Qt::AnchorHorizontalCenter, details, Qt::AnchorHorizontalCenter);
275  anchor = layout->addAnchor(details, Qt::AnchorRight, layout, Qt::AnchorRight);
276 
277  // QGV setup
281  view->show();
282 
283  return app.exec();
284 }
285 
286 #include "main.moc"
GraphicsView(QGraphicsScene *scene, QGraphicsWidget *widget)
Definition: main.cpp:66
QGraphicsWidget * w
Definition: main.cpp:76
void resizeEvent(QResizeEvent *event) override
Definition: main.cpp:71
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const override
Definition: main.cpp:110
~PixmapWidget()
Definition: main.cpp:90
PixmapWidget(const QPixmap &pix)
Definition: main.cpp:82
void setZValue(qreal z)
Definition: main.cpp:96
void setGeometry(const QRectF &rect) override
Definition: main.cpp:101
void resizeEvent(QGraphicsSceneResizeEvent *event) override
Definition: main.cpp:179
PlaceWidget(const QPixmap &pix)
Definition: main.cpp:141
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override
Definition: main.cpp:148
QRectF boundingRect() const override
Definition: main.cpp:186
The QApplication class manages the GUI application's control flow and main settings.
Definition: qapplication.h:68
static int exec()
void setSpacing(int spacing) override
Definition: qboxlayout.cpp:605
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:66
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
void setColorAt(qreal pos, const QColor &color)
Definition: qbrush.cpp:1596
The QGraphicsAnchor class represents an anchor between two items in a QGraphicsAnchorLayout.
void setSpacing(qreal spacing)
The QGraphicsAnchorLayout class provides a layout where one can anchor widgets together in Graphics V...
void setTransform(const QTransform &matrix, bool combine=false)
void setZValue(qreal z)
void setPos(const QPointF &pos)
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts.
void setGraphicsItem(QGraphicsItem *item)
void setSizePolicy(const QSizePolicy &policy)
void setPreferredSize(const QSizeF &size)
void setMinimumSize(const QSizeF &size)
The QGraphicsPixmapItem class provides a pixmap item that you can add to a QGraphicsScene.
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.
void addItem(QGraphicsItem *item)
void setSceneRect(const QRectF &rect)
void setBackgroundBrush(const QBrush &brush)
The QGraphicsSceneResizeEvent class provides events for widget resizing in the graphics view framewor...
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
Definition: qgraphicsview.h:60
QGraphicsScene * scene() const
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.
QSizeF size
the size of the widget
QImage mirrored(bool horizontally=false, bool vertically=true) const &
Definition: qimage.h:254
The QLinearGradient class is used in combination with QBrush to specify a linear gradient brush.
Definition: qbrush.h:430
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect)
Definition: qpainter.cpp:4883
@ CompositionMode_DestinationOver
Definition: qpainter.h:134
The QPalette class contains color groups for each widget state.
Definition: qpalette.h:55
@ Window
Definition: qpalette.h:87
The QPixmap class is an off-screen image representation that can be used as a paint device.
Definition: qpixmap.h:63
int height() const
Definition: qpixmap.cpp:515
QPixmap scaled(int w, int h, Qt::AspectRatioMode aspectMode=Qt::IgnoreAspectRatio, Qt::TransformationMode mode=Qt::FastTransformation) const
Definition: qpixmap.h:114
QImage toImage() const
Definition: qpixmap.cpp:443
QSize size() const
Definition: qpixmap.cpp:528
int width() const
Definition: qpixmap.cpp:503
void fill(const QColor &fillColor=Qt::white)
Definition: qpixmap.cpp:883
static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Definition: qpixmap.cpp:1474
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:242
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:52
The QRectF class defines a finite rectangle in the plane using floating point precision.
Definition: qrect.h:511
The QResizeEvent class contains event parameters for resize events. \inmodule QtGui.
Definition: qevent.h:612
The QSizeF class defines the size of a two-dimensional object using floating point precision.
Definition: qsize.h:235
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:55
constexpr int height() const noexcept
Definition: qsize.h:160
constexpr void setHeight(int h) noexcept
Definition: qsize.h:166
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem.
Definition: qstyleoption.h:684
static QTransform fromScale(qreal dx, qreal dy)
Definition: qtransform.cpp:503
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
void setLayout(QLayout *)
Definition: qwidget.cpp:10146
void setPalette(const QPalette &)
Definition: qwidget.cpp:4536
int main(int argc, char **argv)
Definition: main.cpp:1
QOpenGLWidget * widget
[1]
QPixmap p2
QPixmap p1
[0]
rect
[4]
QPixmap pix
@ white
Definition: qnamespace.h:62
@ transparent
Definition: qnamespace.h:78
@ black
Definition: qnamespace.h:61
@ AnchorRight
Definition: qnamespace.h:1472
@ AnchorVerticalCenter
Definition: qnamespace.h:1474
@ AnchorBottom
Definition: qnamespace.h:1475
@ AnchorTop
Definition: qnamespace.h:1473
@ AnchorHorizontalCenter
Definition: qnamespace.h:1471
@ AnchorLeft
Definition: qnamespace.h:1470
@ Window
Definition: qnamespace.h:232
SizeHint
Definition: qnamespace.h:1589
@ MaximumSize
Definition: qnamespace.h:1592
@ PreferredSize
Definition: qnamespace.h:1591
@ MinimumSize
Definition: qnamespace.h:1590
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
#define Q_INIT_RESOURCE(name)
Definition: qglobal.h:582
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat z
GLboolean r
[2]
GLfloat GLfloat GLfloat w
[0]
GLenum GLuint GLintptr GLsizeiptr size
[1]
struct _cl_event * event
Definition: qopenglext.h:2998
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
#define Q_OBJECT
Definition: qtmetamacros.h:158
#define QWIDGETSIZE_MAX
Definition: qwidget.h:951
Q_UNUSED(salary)
[21]
QVBoxLayout * layout
QString title
[35]
QGraphicsScene scene
[0]
QApplication app(argc, argv)
[0]
QPainter painter(this)
[7]
QQuickView * view
[0]