QtBase  v6.3.1
windowcontainer.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 "openglwindow.h"
52 
53 #include <QApplication>
54 #include <QFocusEvent>
55 #include <QHBoxLayout>
56 #include <QKeyEvent>
57 #include <QLineEdit>
58 #include <QMouseEvent>
59 #include <QPainter>
60 #include <QWidget>
61 
62 
63 // Making use of the class from the openglwindow example
64 class Window : public OpenGLWindow
65 {
66  Q_OBJECT
67 public:
69 
70  void render(QPainter *p) override
71  {
72  QLinearGradient g(0, 0, 0, height());
73  g.setColorAt(0, QColor("lightsteelblue"));
74  g.setColorAt(1, Qt::black);
75  p->fillRect(0, 0, width(), height(), g);
76 
77  p->setPen(Qt::white);
78 
79  p->drawText(20, 30, QLatin1String("This is an OpenGL based QWindow"));
80 
81  if (m_key.trimmed().length() > 0) {
82  QRect bounds = p->boundingRect(QRect(0, 0, width(), height()), Qt::AlignTop | Qt::AlignLeft, m_key);
83  p->save();
84  p->translate(width() / 2.0, height() / 2.0);
85  p->scale(10, 10);
86  p->translate(-bounds.width() / 2.0, -bounds.height() / 2.0);
87  p->drawText(bounds, Qt::AlignCenter, m_key);
88  p->restore();
89  }
90 
91  if (m_focus)
92  p->drawText(20, height() - 20, QLatin1String("Window has focus!"));
93 
94  p->setRenderHint(QPainter::Antialiasing);
95  p->drawPolyline(m_polygon);
96  }
97 
98  void mousePressEvent(QMouseEvent *e) override
99  {
100  if (!m_mouseDown) {
101  m_mouseDown = true;
102  m_polygon.clear();
103  m_polygon.append(e->position().toPoint());
104  renderLater();
105  }
106  }
107 
108  void mouseMoveEvent(QMouseEvent *e) override
109  {
110  if (m_mouseDown) {
111  m_polygon.append(e->position().toPoint());
112  renderLater();
113  }
114  }
115 
117  {
118  if (m_mouseDown) {
119  m_mouseDown = false;
120  m_polygon.append(e->position().toPoint());
121  renderLater();
122  }
123  }
124 
125  void focusInEvent(QFocusEvent *) override
126  {
127  m_focus = true;
128  renderLater();
129  }
130 
131  void focusOutEvent(QFocusEvent *) override
132  {
133  m_focus = false;
134  m_polygon.clear();
135  renderLater();
136  }
137 
138  void keyPressEvent(QKeyEvent *e) override
139  {
140  m_key = e->text();
141  renderLater();
142  }
143 
144  void keyReleaseEvent(QKeyEvent *) override
145  {
146  m_key = QString();
147  renderLater();
148  }
149 private:
150  QPolygon m_polygon;
151  QString m_key;
152  bool m_mouseDown = false;
153  bool m_focus = false;
154 };
155 
156 
157 int main(int argc, char *argv[])
158 {
159  QApplication app(argc, argv);
160 
161  QWidget *widget = new QWidget;
163 
164  Window *window = new Window;
165 
167  container->setMinimumSize(300, 300);
168  container->setMaximumSize(600, 600);
170  container->setFocusPolicy(Qt::StrongFocus);
171 
172  window->setGeometry(100, 100, 300, 200);
173 
174  layout->addWidget(new QLineEdit(QLatin1String("A QLineEdit")));
175  layout->addWidget(container);
176  layout->addWidget(new QLineEdit(QLatin1String("A QLabel")));
177 
178  widget->show();
179 
180  return app.exec();
181 }
182 
183 #include "windowcontainer.moc"
OpenGLWindow()
Definition: main.cpp:108
void renderLater()
[2]
The QApplication class manages the GUI application's control flow and main settings.
Definition: qapplication.h:68
static int exec()
void addWidget(QWidget *, int stretch=0, Qt::Alignment alignment=Qt::Alignment())
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
The QFocusEvent class contains event parameters for widget focus events. \inmodule QtGui.
Definition: qevent.h:520
The QHBoxLayout class lines up widgets horizontally.
Definition: qboxlayout.h:114
The QKeyEvent class describes a key event.
Definition: qevent.h:471
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
The QLineEdit widget is a one-line text editor.
Definition: qlineedit.h:64
The QLinearGradient class is used in combination with QBrush to specify a linear gradient brush.
Definition: qbrush.h:430
void append(parameter_type t)
Definition: qlist.h:469
void clear()
Definition: qlist.h:445
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:231
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
@ Antialiasing
Definition: qpainter.h:88
The QPolygon class provides a list of points using integer precision. \inmodule QtGui.
Definition: qpolygon.h:57
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:59
constexpr int height() const noexcept
Definition: qrect.h:266
constexpr void translate(int dx, int dy) noexcept
Definition: qrect.h:272
constexpr int width() const noexcept
Definition: qrect.h:263
The QString class provides a Unicode character string.
Definition: qstring.h:388
QString trimmed() const &
Definition: qstring.h:623
qsizetype length() const
Definition: qstring.h:415
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
void setMinimumSize(const QSize &)
Definition: qwidget.h:865
void setSizePolicy(QSizePolicy)
void setFocusPolicy(Qt::FocusPolicy policy)
Definition: qwidget.cpp:7773
void show()
Definition: qwidget.cpp:7825
static QWidget * createWindowContainer(QWindow *window, QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags())
void setMaximumSize(const QSize &)
Definition: qwidget.h:868
int width
the width of the window's geometry
Definition: qwindow.h:118
int height
the height of the window's geometry
Definition: qwindow.h:119
[Window class definition]
Definition: window.h:64
void focusInEvent(QFocusEvent *) override
void keyReleaseEvent(QKeyEvent *) override
void mouseMoveEvent(QMouseEvent *e) override
void focusOutEvent(QFocusEvent *) override
void keyPressEvent(QKeyEvent *e) override
void render(QPainter *p) override
[2]
void mouseReleaseEvent(QMouseEvent *e) override
void mousePressEvent(QMouseEvent *e) override
QOpenGLWidget * widget
[1]
double e
@ AlignTop
Definition: qnamespace.h:178
@ AlignCenter
Definition: qnamespace.h:188
@ AlignLeft
Definition: qnamespace.h:169
@ StrongFocus
Definition: qnamespace.h:135
@ white
Definition: qnamespace.h:62
@ black
Definition: qnamespace.h:61
#define QString()
Definition: parse-defines.h:51
GLboolean GLboolean g
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
#define Q_OBJECT
Definition: qtmetamacros.h:158
struct Window Window
Definition: sqlite3.c:14312
QVBoxLayout * layout
QApplication app(argc, argv)
[0]
aWidget window() -> setWindowTitle("New Window Title")
[2]
int main(int argc, char *argv[])
[1]