QtBase  v6.3.1
windowtransparency.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include <QtGui>
30 #include <QtWidgets>
31 
32 class GLWindow : public QWindow
33 {
34 public:
35  GLWindow(Qt::WindowFlags flags)
36  : gl(0)
37  {
38  setFlags(flags);
39  setSurfaceType(OpenGLSurface);
40 
43  format.setSamples(16);
45  }
46 
48  {
49  if (!isExposed())
50  return;
51 
52  if (!gl) {
53  gl = new QOpenGLContext();
54  gl->setFormat(requestedFormat());
55  gl->create();
56  }
57 
58  gl->makeCurrent(this);
59 
62  "attribute highp vec4 a_Pos;"
63  "attribute lowp vec4 a_Color;"
64  "varying lowp vec4 v_Color;"
65  "void main() {"
66  " gl_Position = a_Pos;"
67  " v_Color = a_Color;"
68  "}");
70  "varying lowp vec4 v_Color;"
71  "void main() {"
72  " gl_FragColor = v_Color;"
73  "}");
74  prog.bind();
75 
76  QOpenGLFunctions *functions = gl->functions();
77  functions->glClearColor(0, 0, 0, 0);
78  functions->glClear(GL_COLOR_BUFFER_BIT);
79  functions->glViewport(0, 0, width(), height());
80 
81  prog.enableAttributeArray("a_Pos");
82  prog.enableAttributeArray("a_Color");
83 
84  float coords[] = { -0.7f, 0.7f,
85  0.8f, 0.8f,
86  -0.8f, -0.8f,
87  0.7f, -0.7f };
88  float colors[] = { 1, 0, 0, 1,
89  0, 1, 0, 1,
90  0, 0, 1, 1,
91  0, 0, 0, 0 };
92 
93  prog.setAttributeArray("a_Pos", GL_FLOAT, coords, 2, 0);
94  prog.setAttributeArray("a_Color", GL_FLOAT, colors, 4, 0);
95 
96  functions->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
97 
98  prog.disableAttributeArray("a_Pos");
99  prog.disableAttributeArray("a_Color");
100 
101  gl->swapBuffers(this);
102  }
103 
105  {
107  }
108 
109 private:
110  QOpenGLContext *gl;
111 };
112 
113 class Widget : public QWidget
114 {
115 public:
116  Widget(Qt::WindowFlags flags)
117  : QWidget(0, flags)
118  {
120  }
121 
123  {
124  QPainter p(this);
125 
126  p.setRenderHint(QPainter::Antialiasing);
127 
128  p.fillRect(rect(), QColor("steelblue"));
129 
130  int w = width();
131  int h = height();
132  int w2 = width() / 2;
133  int h2 = height() / 2;
134 
136  path.moveTo(0, 0);
137  path.cubicTo(w2, 0, w2, h2, w, h);
138  path.cubicTo(w2, h, w2, h2, 0, 0);
139 
140  QLinearGradient lg(0, 0, w, h);
141  lg.setColorAt(0.0, Qt::transparent);
142  lg.setColorAt(0.5, QColor("palegreen"));
143  lg.setColorAt(1.0, Qt::transparent);
144 
145  p.setCompositionMode(QPainter::CompositionMode_Source);
146  p.fillPath(path, lg);
147  }
148 
150  {
152  }
153 
154 };
155 
156 int main(int argc, char **argv)
157 {
158  QApplication app(argc, argv);
159 
160  // mask: on/off
161  // opacity: on/off
162 
163  for (int i=0; i<4; ++i) {
164  bool mask = i & 0x1;
165  bool opacity = i & 0x2;
166 
167  Qt::WindowFlags flags = Qt::FramelessWindowHint;
168 
169  Widget *widget = new Widget(flags);
170  GLWindow *window = new GLWindow(flags);
171 
172  widget->setGeometry(100 + 100 * i, 100, 80, 80);
173  window->setGeometry(100 + 100 * i, 200, 80, 80);
174  if (mask) {
175  QRegion region(0, 0, 80, 80, QRegion::Ellipse);
176  widget->setMask(region);
177  window->setMask(region);
178  }
179  if (opacity) {
180  widget->setWindowOpacity(0.5);
181  window->setOpacity(0.5);
182  }
183 
184  widget->show();
185  window->show();
186  }
187 
188  return app.exec();
189 }
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
GLWindow(Qt::WindowFlags flags)
void mousePressEvent(QMouseEvent *)
void exposeEvent(QExposeEvent *)
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
The QExposeEvent class contains event parameters for expose events. \inmodule QtGui.
Definition: qevent.h:574
void setColorAt(qreal pos, const QColor &color)
Definition: qbrush.cpp:1596
void setAttribute(Qt::WidgetAttribute attribute, bool on=true)
The QLinearGradient class is used in combination with QBrush to specify a linear gradient brush.
Definition: qbrush.h:430
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:231
The QOpenGLContext class represents a native OpenGL context, enabling OpenGL rendering on a QSurface.
bool makeCurrent(QSurface *surface)
void setFormat(const QSurfaceFormat &format)
QOpenGLFunctions * functions() const
void swapBuffers(QSurface *surface)
The QOpenGLFunctions class provides cross-platform access to the OpenGL ES 2.0 API.
void glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
void glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
void glDrawArrays(GLenum mode, GLint first, GLsizei count)
void glClear(GLbitfield mask)
The QOpenGLShaderProgram class allows OpenGL shader programs to be linked and used.
bool addShaderFromSourceCode(QOpenGLShader::ShaderType type, const char *source)
void enableAttributeArray(int location)
void disableAttributeArray(int location)
void setAttributeArray(int location, const GLfloat *values, int tupleSize, int stride=0)
The QPaintEvent class contains event parameters for paint events. \inmodule QtGui.
Definition: qevent.h:539
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
@ Antialiasing
Definition: qpainter.h:88
@ CompositionMode_Source
Definition: qpainter.h:136
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:65
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:63
@ Ellipse
Definition: qregion.h:65
The QSurfaceFormat class represents the format of a QSurface. \inmodule QtGui.
void setAlphaBufferSize(int size)
@ OpenGLSurface
Definition: qsurface.h:68
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
void setGeometry(int x, int y, int w, int h)
Definition: qwidget.h:919
void setMask(const QBitmap &)
void setWindowOpacity(qreal level)
Definition: qwidget.cpp:11330
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
QRect rect
the internal geometry of the widget excluding any window frame
Definition: qwidget.h:150
void show()
Definition: qwidget.cpp:7825
The QWindow class represents a window in the underlying windowing system.
Definition: qwindow.h:99
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
Definition: widget.h:60
void mousePressEvent(QMouseEvent *)
Widget(Qt::WindowFlags flags)
void paintEvent(QPaintEvent *)
QOpenGLWidget * widget
[1]
widget setFormat(format)
@ WA_TranslucentBackground
Definition: qnamespace.h:427
@ transparent
Definition: qnamespace.h:78
@ Widget
Definition: qnamespace.h:231
@ FramelessWindowHint
Definition: qnamespace.h:250
GLfloat GLfloat GLfloat w
[0]
GLsizei const GLubyte GLsizei GLenum const void * coords
GLbitfield flags
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
GLint GLsizei GLsizei GLenum format
GLfloat GLfloat GLfloat GLfloat h
GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint GLdouble GLdouble w2
Definition: qopenglext.h:12395
GLsizei const GLchar *const * path
Definition: qopenglext.h:4283
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
QApplication app(argc, argv)
[0]
aWidget window() -> setWindowTitle("New Window Title")
[2]
int main(int argc, char **argv)
[1]