Sophie

Sophie

distrib > Mageia > 6 > armv5tl > media > core-updates > by-pkgid > 768f7d9f703884aa2562bf0a651086df > files > 3449

qtbase5-doc-5.9.4-1.1.mga6.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qopenglwidget.cpp -->
  <title>QOpenGLWidget Class | Qt Widgets 5.9</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.9</td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-module.html">C++ Classes</a></td><td >QOpenGLWidget</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#public-types">Public Types</a></li>
<li class="level1"><a href="#public-functions">Public Functions</a></li>
<li class="level1"><a href="#signals">Signals</a></li>
<li class="level1"><a href="#protected-functions">Protected Functions</a></li>
<li class="level1"><a href="#reimplemented-protected-functions">Reimplemented Protected Functions</a></li>
<li class="level1"><a href="#details">Detailed Description</a></li>
<li class="level2"><a href="#painting-techniques">Painting Techniques</a></li>
<li class="level2"><a href="#opengl-function-calls-headers-and-qopenglfunctions">OpenGL Function Calls, Headers and QOpenGLFunctions</a></li>
<li class="level2"><a href="#code-examples">Code Examples</a></li>
<li class="level2"><a href="#relation-to-qglwidget">Relation to QGLWidget</a></li>
<li class="level2"><a href="#differences-to-qglwidget">Differences to QGLWidget</a></li>
<li class="level2"><a href="#multisampling">Multisampling</a></li>
<li class="level2"><a href="#threading">Threading</a></li>
<li class="level2"><a href="#context-sharing">Context Sharing</a></li>
<li class="level2"><a href="#resource-initialization-and-cleanup">Resource Initialization and Cleanup</a></li>
<li class="level2"><a href="#limitations">Limitations</a></li>
<li class="level2"><a href="#alternatives">Alternatives</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QOpenGLWidget Class</h1>
<!-- $$$QOpenGLWidget-brief -->
<p>The <a href="qopenglwidget.html">QOpenGLWidget</a> class is a widget for rendering OpenGL graphics. <a href="#details">More...</a></p>
<!-- @@@QOpenGLWidget -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> Header:</td><td class="memItemRight bottomAlign">   <span class="preprocessor">#include &lt;QOpenGLWidget&gt;</span>
</td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += widgets</td></tr><tr><td class="memItemLeft rightAlign topAlign"> Since:</td><td class="memItemRight bottomAlign">  Qt 5.4</td></tr><tr><td class="memItemLeft rightAlign topAlign"> Inherits:</td><td class="memItemRight bottomAlign"> <a href="qwidget.html">QWidget</a></td></tr></table></div><ul>
<li><a href="qopenglwidget-members.html">List of all members, including inherited members</a></li>
</ul>
<a name="public-types"></a>
<h2 id="public-types">Public Types</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> enum </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#UpdateBehavior-enum">UpdateBehavior</a></b> { NoPartialUpdate, PartialUpdate }</td></tr>
</table></div>
<a name="public-functions"></a>
<h2 id="public-functions">Public Functions</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#QOpenGLWidget">QOpenGLWidget</a></b>(QWidget *<i>parent</i> = Q_NULLPTR, Qt::WindowFlags <i>f</i> = Qt::WindowFlags())</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#dtor.QOpenGLWidget">~QOpenGLWidget</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QOpenGLContext *</td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#context">context</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> GLuint </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#defaultFramebufferObject">defaultFramebufferObject</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#doneCurrent">doneCurrent</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QSurfaceFormat </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#format">format</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QImage </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#grabFramebuffer">grabFramebuffer</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#isValid">isValid</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#makeCurrent">makeCurrent</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#setFormat">setFormat</a></b>(const QSurfaceFormat &amp;<i>format</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#setUpdateBehavior">setUpdateBehavior</a></b>(UpdateBehavior <i>updateBehavior</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> UpdateBehavior </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#updateBehavior">updateBehavior</a></b>() const</td></tr>
</table></div>
<ul>
<li class="fn">214 public functions inherited from <a href="qwidget.html#public-functions">QWidget</a></li>
<li class="fn">32 public functions inherited from <a href="../qtcore/qobject.html#public-functions">QObject</a></li>
<li class="fn">14 public functions inherited from <a href="../qtgui/qpaintdevice.html#public-functions">QPaintDevice</a></li>
</ul>
<a name="signals"></a>
<h2 id="signals">Signals</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#aboutToCompose">aboutToCompose</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#aboutToResize">aboutToResize</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#frameSwapped">frameSwapped</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#resized">resized</a></b>()</td></tr>
</table></div>
<ul>
<li class="fn">3 signals inherited from <a href="qwidget.html#signals">QWidget</a></li>
<li class="fn">2 signals inherited from <a href="../qtcore/qobject.html#signals">QObject</a></li>
</ul>
<a name="protected-functions"></a>
<h2 id="protected-functions">Protected Functions</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> virtual void </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#initializeGL">initializeGL</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual void </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#paintGL">paintGL</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual void </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#resizeGL">resizeGL</a></b>(int <i>w</i>, int <i>h</i>)</td></tr>
</table></div>
<a name="reimplemented-protected-functions"></a>
<h2 id="reimplemented-protected-functions">Reimplemented Protected Functions</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> virtual void </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#paintEvent">paintEvent</a></b>(QPaintEvent *<i>e</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual void </td><td class="memItemRight bottomAlign"><b><a href="qopenglwidget.html#resizeEvent">resizeEvent</a></b>(QResizeEvent *<i>e</i>)</td></tr>
</table></div>
<ul>
<li class="fn">35 protected functions inherited from <a href="qwidget.html#protected-functions">QWidget</a></li>
<li class="fn">9 protected functions inherited from <a href="../qtcore/qobject.html#protected-functions">QObject</a></li>
<li class="fn">1 protected function inherited from <a href="../qtgui/qpaintdevice.html#protected-functions">QPaintDevice</a></li>
</ul>
<h3>Additional Inherited Members</h3>
<ul>
<li class="fn">59 properties inherited from <a href="qwidget.html#properties">QWidget</a></li>
<li class="fn">1 property inherited from <a href="../qtcore/qobject.html#properties">QObject</a></li>
<li class="fn">19 public slots inherited from <a href="qwidget.html#public-slots">QWidget</a></li>
<li class="fn">1 public slot inherited from <a href="../qtcore/qobject.html#public-slots">QObject</a></li>
<li class="fn">1 public variable inherited from <a href="../qtcore/qobject.html#public-variables">QObject</a></li>
<li class="fn">5 static public members inherited from <a href="qwidget.html#static-public-members">QWidget</a></li>
<li class="fn">10 static public members inherited from <a href="../qtcore/qobject.html#static-public-members">QObject</a></li>
<li class="fn">1 protected slot inherited from <a href="qwidget.html#protected-slots">QWidget</a></li>
<li class="fn">2 protected variables inherited from <a href="../qtcore/qobject.html#protected-variables">QObject</a></li>
<li class="fn">1 protected type inherited from <a href="../qtgui/qpaintdevice.html#protected-variables">QPaintDevice</a></li>
</ul>
<a name="details"></a>
<!-- $$$QOpenGLWidget-description -->
<div class="descr">
<h2 id="details">Detailed Description</h2>
<p>The <a href="qopenglwidget.html">QOpenGLWidget</a> class is a widget for rendering OpenGL graphics.</p>
<p><a href="qopenglwidget.html">QOpenGLWidget</a> provides functionality for displaying OpenGL graphics integrated into a Qt application. It is very simple to use: Make your class inherit from it and use the subclass like any other <a href="qwidget.html">QWidget</a>, except that you have the choice between using <a href="../qtgui/qpainter.html">QPainter</a> and standard OpenGL rendering commands.</p>
<p><a href="qopenglwidget.html">QOpenGLWidget</a> provides three convenient virtual functions that you can reimplement in your subclass to perform the typical OpenGL tasks:</p>
<ul>
<li><a href="qopenglwidget.html#paintGL">paintGL</a>() - Renders the OpenGL scene. Gets called whenever the widget needs to be updated.</li>
<li><a href="qopenglwidget.html#resizeGL">resizeGL</a>() - Sets up the OpenGL viewport, projection, etc. Gets called whenever the widget has been resized (and also when it is shown for the first time because all newly created widgets get a resize event automatically).</li>
<li><a href="qopenglwidget.html#initializeGL">initializeGL</a>() - Sets up the OpenGL resources and state. Gets called once before the first time <a href="qopenglwidget.html#resizeGL">resizeGL</a>() or <a href="qopenglwidget.html#paintGL">paintGL</a>() is called.</li>
</ul>
<p>If you need to trigger a repaint from places other than <a href="qopenglwidget.html#paintGL">paintGL</a>() (a typical example is when using <a href="../qtcore/qtimer.html">timers</a> to animate scenes), you should call the widget's <a href="qwidget.html#update">update</a>() function to schedule an update.</p>
<p>Your widget's OpenGL rendering context is made current when <a href="qopenglwidget.html#paintGL">paintGL</a>(), <a href="qopenglwidget.html#resizeGL">resizeGL</a>(), or <a href="qopenglwidget.html#initializeGL">initializeGL</a>() is called. If you need to call the standard OpenGL API functions from other places (e.g&#x2e; in your widget's constructor or in your own paint functions), you must call <a href="qopenglwidget.html#makeCurrent">makeCurrent</a>() first.</p>
<p>All rendering happens into an OpenGL framebuffer object. <a href="qopenglwidget.html#makeCurrent">makeCurrent</a>() ensure that it is bound in the context. Keep this in mind when creating and binding additional framebuffer objects in the rendering code in <a href="qopenglwidget.html#paintGL">paintGL</a>(). Never re-bind the framebuffer with ID 0. Instead, call <a href="qopenglwidget.html#defaultFramebufferObject">defaultFramebufferObject</a>() to get the ID that should be bound.</p>
<p><a href="qopenglwidget.html">QOpenGLWidget</a> allows using different OpenGL versions and profiles when the platform supports it. Just set the requested format via <a href="qopenglwidget.html#setFormat">setFormat</a>(). Keep in mind however that having multiple <a href="qopenglwidget.html">QOpenGLWidget</a> instances in the same window requires that they all use the same format, or at least formats that do not make the contexts non-sharable. To overcome this issue, prefer using <a href="../qtgui/qsurfaceformat.html#setDefaultFormat">QSurfaceFormat::setDefaultFormat</a>() instead of <a href="qopenglwidget.html#setFormat">setFormat</a>().</p>
<p><b>Note: </b>Calling <a href="../qtgui/qsurfaceformat.html#setDefaultFormat">QSurfaceFormat::setDefaultFormat</a>() before constructing the <a href="qapplication.html">QApplication</a> instance is mandatory on some platforms (for example, macOS) when an OpenGL core profile context is requested. This is to ensure that resource sharing between contexts stays functional as all internal contexts are created using the correct version and profile.</p><a name="painting-techniques"></a>
<h3 >Painting Techniques</h3>
<p>As described above, subclass <a href="qopenglwidget.html">QOpenGLWidget</a> to render pure 3D content in the following way:</p>
<ul>
<li>Reimplement the <a href="qopenglwidget.html#initializeGL">initializeGL</a>() and <a href="qopenglwidget.html#resizeGL">resizeGL</a>() functions to set up the OpenGL state and provide a perspective transformation.</li>
<li>Reimplement <a href="qopenglwidget.html#paintGL">paintGL</a>() to paint the 3D scene, calling only OpenGL functions.</li>
</ul>
<p>It is also possible to draw 2D graphics onto a <a href="qopenglwidget.html">QOpenGLWidget</a> subclass using <a href="../qtgui/qpainter.html">QPainter</a>:</p>
<ul>
<li>In <a href="qopenglwidget.html#paintGL">paintGL</a>(), instead of issuing OpenGL commands, construct a <a href="../qtgui/qpainter.html">QPainter</a> object for use on the widget.</li>
<li>Draw primitives using <a href="../qtgui/qpainter.html">QPainter</a>'s member functions.</li>
<li>Direct OpenGL commands can still be issued. However, you must make sure these are enclosed by a call to the painter's beginNativePainting() and endNativePainting().</li>
</ul>
<p>When performing drawing using <a href="../qtgui/qpainter.html">QPainter</a> only, it is also possible to perform the painting like it is done for ordinary widgets: by reimplementing <a href="qopenglwidget.html#paintEvent">paintEvent</a>().</p>
<ul>
<li>Reimplement the <a href="qopenglwidget.html#paintEvent">paintEvent</a>() function.</li>
<li>Construct a <a href="../qtgui/qpainter.html">QPainter</a> object targeting the widget. Either pass the widget to the constructor or the <a href="../qtgui/qpainter.html#begin">QPainter::begin</a>() function.</li>
<li>Draw primitives using <a href="../qtgui/qpainter.html">QPainter</a>'s member functions.</li>
<li>Painting finishes then the <a href="../qtgui/qpainter.html">QPainter</a> instance is destroyed. Alternatively, call <a href="../qtgui/qpainter.html#end">QPainter::end</a>() explicitly.</li>
</ul>
<a name="opengl-function-calls-headers-and-qopenglfunctions"></a>
<h3 >OpenGL Function Calls, Headers and QOpenGLFunctions</h3>
<p>When making OpenGL function calls, it is strongly recommended to avoid calling the functions directly. Instead, prefer using <a href="../qtgui/qopenglfunctions.html">QOpenGLFunctions</a> (when making portable applications) or the versioned variants (for example, <a href="../qtgui/qopenglfunctions-3-2-core.html">QOpenGLFunctions_3_2_Core</a> and similar, when targeting modern, desktop-only OpenGL). This way the application will work correctly in all Qt build configurations, including the ones that perform dynamic OpenGL implementation loading which means applications are not directly linking to an GL implementation and thus direct function calls are not feasible.</p>
<p>In <a href="qopenglwidget.html#paintGL">paintGL</a>() the current context is always accessible by caling <a href="../qtgui/qopenglcontext.html#currentContext">QOpenGLContext::currentContext</a>(). From this context an already initialized, ready-to-be-used <a href="../qtgui/qopenglfunctions.html">QOpenGLFunctions</a> instance is retrievable by calling <a href="../qtgui/qopenglcontext.html#functions">QOpenGLContext::functions</a>(). An alternative to prefixing every GL call is to inherit from <a href="../qtgui/qopenglfunctions.html">QOpenGLFunctions</a> and call <a href="../qtgui/qopenglfunctions.html#initializeOpenGLFunctions">QOpenGLFunctions::initializeOpenGLFunctions</a>() in <a href="qopenglwidget.html#initializeGL">initializeGL</a>().</p>
<p>As for the OpenGL headers, note that in most cases there will be no need to directly include any headers like GL.h&#x2e; The OpenGL-related Qt headers will include qopengl.h which will in turn include an appropriate header for the system. This might be an OpenGL ES 3.x or 2.0 header, the highest version that is available, or a system-provided gl.h&#x2e; In addition, a copy of the extension headers (called glext.h on some systems) is provided as part of Qt both for OpenGL and OpenGL ES. These will get included automatically on platforms where feasible. This means that constants and function pointer typedefs from ARB, EXT, OES extensions are automatically available.</p>
<a name="code-examples"></a>
<h3 >Code Examples</h3>
<p>To get started, the simplest <a href="qopenglwidget.html">QOpenGLWidget</a> subclass could like like the following:</p>
<pre class="cpp">

  <span class="keyword">class</span> MyGLWidget : <span class="keyword">public</span> <span class="type"><a href="qopenglwidget.html#QOpenGLWidget">QOpenGLWidget</a></span>
  {
  <span class="keyword">public</span>:
      MyGLWidget(<span class="type"><a href="qwidget.html#QWidget">QWidget</a></span> <span class="operator">*</span>parent) : <span class="type"><a href="qopenglwidget.html#QOpenGLWidget">QOpenGLWidget</a></span>(parent) { }

  <span class="keyword">protected</span>:
      <span class="type">void</span> initializeGL()
      {
          <span class="comment">// Set up the rendering context, load shaders and other resources, etc.:</span>
          <span class="type"><a href="../qtgui/qopenglfunctions.html">QOpenGLFunctions</a></span> <span class="operator">*</span>f <span class="operator">=</span> <span class="type"><a href="../qtgui/qopenglcontext.html">QOpenGLContext</a></span><span class="operator">::</span>currentContext()<span class="operator">-</span><span class="operator">&gt;</span>functions();
          f<span class="operator">-</span><span class="operator">&gt;</span>glClearColor(<span class="number">1.0f</span><span class="operator">,</span> <span class="number">1.0f</span><span class="operator">,</span> <span class="number">1.0f</span><span class="operator">,</span> <span class="number">1.0f</span>);
          <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
      }

      <span class="type">void</span> resizeGL(<span class="type">int</span> w<span class="operator">,</span> <span class="type">int</span> h)
      {
          <span class="comment">// Update projection matrix and other size related settings:</span>
          m_projection<span class="operator">.</span>setToIdentity();
          m_projection<span class="operator">.</span>perspective(<span class="number">45.0f</span><span class="operator">,</span> w <span class="operator">/</span> <span class="type">float</span>(h)<span class="operator">,</span> <span class="number">0.01f</span><span class="operator">,</span> <span class="number">100.0f</span>);
          <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
      }

      <span class="type">void</span> paintGL()
      {
          <span class="comment">// Draw the scene:</span>
          <span class="type"><a href="../qtgui/qopenglfunctions.html">QOpenGLFunctions</a></span> <span class="operator">*</span>f <span class="operator">=</span> <span class="type"><a href="../qtgui/qopenglcontext.html">QOpenGLContext</a></span><span class="operator">::</span>currentContext()<span class="operator">-</span><span class="operator">&gt;</span>functions();
          f<span class="operator">-</span><span class="operator">&gt;</span>glClear(GL_COLOR_BUFFER_BIT);
          <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
      }

  };

</pre>
<p>Alternatively, the prefixing of each and every OpenGL call can be avoided by deriving from <a href="../qtgui/qopenglfunctions.html">QOpenGLFunctions</a> instead:</p>
<pre class="cpp">

  <span class="keyword">class</span> MyGLWidget : <span class="keyword">public</span> <span class="type"><a href="qopenglwidget.html#QOpenGLWidget">QOpenGLWidget</a></span><span class="operator">,</span> <span class="keyword">protected</span> <span class="type"><a href="../qtgui/qopenglfunctions.html">QOpenGLFunctions</a></span>
  {
      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
      <span class="type">void</span> initializeGL()
      {
          initializeOpenGLFunctions();
          glClearColor(<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>);
          <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
      }
      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
  };

</pre>
<p>To get a context compatible with a given OpenGL version or profile, or to request depth and stencil buffers, call <a href="qopenglwidget.html#setFormat">setFormat</a>():</p>
<pre class="cpp">

  <span class="type"><a href="qopenglwidget.html#QOpenGLWidget">QOpenGLWidget</a></span> <span class="operator">*</span>widget <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qopenglwidget.html#QOpenGLWidget">QOpenGLWidget</a></span>(parent);
  <span class="type"><a href="../qtgui/qsurfaceformat.html">QSurfaceFormat</a></span> format;
  format<span class="operator">.</span>setDepthBufferSize(<span class="number">24</span>);
  format<span class="operator">.</span>setStencilBufferSize(<span class="number">8</span>);
  format<span class="operator">.</span>setVersion(<span class="number">3</span><span class="operator">,</span> <span class="number">2</span>);
  format<span class="operator">.</span>setProfile(<span class="type"><a href="../qtgui/qsurfaceformat.html">QSurfaceFormat</a></span><span class="operator">::</span>CoreProfile);
  widget<span class="operator">-</span><span class="operator">&gt;</span>setFormat(format); <span class="comment">// must be called before the widget or its parent window gets shown</span>

</pre>
<p>With OpenGL 3.0+ contexts, when portability is not important, the versioned <a href="../qtgui/qopenglfunctions.html">QOpenGLFunctions</a> variants give easy access to all the modern OpenGL functions available in a given version:</p>
<pre class="cpp">

      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
      <span class="type">void</span> paintGL()
      {
          QOpenGLFunctions_3_2_Core <span class="operator">*</span>f <span class="operator">=</span> <span class="type"><a href="../qtgui/qopenglcontext.html">QOpenGLContext</a></span><span class="operator">::</span>currentContext()<span class="operator">-</span><span class="operator">&gt;</span>versionFunctions<span class="operator">&lt;</span>QOpenGLFunctions_3_2_Core<span class="operator">&gt;</span>();
          <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
          f<span class="operator">-</span><span class="operator">&gt;</span>glDrawArraysInstanced(<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>);
          <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
      }
      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>

</pre>
<p>As described above, it is simpler and more robust to set the requested format globally so that it applies to all windows and contexts during the lifetime of the application. Below is an example of this:</p>
<pre class="cpp">

  <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span><span class="operator">*</span>argv)
  {
      <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);

      <span class="type"><a href="../qtgui/qsurfaceformat.html">QSurfaceFormat</a></span> format;
      format<span class="operator">.</span>setDepthBufferSize(<span class="number">24</span>);
      format<span class="operator">.</span>setStencilBufferSize(<span class="number">8</span>);
      format<span class="operator">.</span>setVersion(<span class="number">3</span><span class="operator">,</span> <span class="number">2</span>);
      format<span class="operator">.</span>setProfile(<span class="type"><a href="../qtgui/qsurfaceformat.html">QSurfaceFormat</a></span><span class="operator">::</span>CoreProfile);
      <span class="type"><a href="../qtgui/qsurfaceformat.html">QSurfaceFormat</a></span><span class="operator">::</span>setDefaultFormat(format);

      MyWidget widget;
      widget<span class="operator">.</span>show();

      <span class="keyword">return</span> app<span class="operator">.</span>exec();
  }

</pre>
<a name="relation-to-qglwidget"></a>
<h3 >Relation to QGLWidget</h3>
<p>The legacy QtOpenGL module (classes prefixed with QGL) provides a widget called QGLWidget. <a href="qopenglwidget.html">QOpenGLWidget</a> is intended to be a modern replacement for it. Therefore, especially in new applications, the general recommendation is to use <a href="qopenglwidget.html">QOpenGLWidget</a>.</p>
<p>While the API is very similar, there is an important difference between the two: <a href="qopenglwidget.html">QOpenGLWidget</a> always renders offscreen, using framebuffer objects. QGLWidget on the other hand uses a native window and surface. The latter causes issues when using it in complex user interfaces since, depending on the platform, such native child widgets may have various limitations, regarding stacking orders for example. <a href="qopenglwidget.html">QOpenGLWidget</a> avoids this by not creating a separate native window.</p>
<p>Due to being backed by a framebuffer object, the behavior of <a href="qopenglwidget.html">QOpenGLWidget</a> is very similar to <a href="../qtgui/qopenglwindow.html">QOpenGLWindow</a> with the update behavior set to <code>PartialUpdateBlit</code> or <code>PartialUpdateBlend</code>. This means that the contents are preserved between <a href="qopenglwidget.html#paintGL">paintGL</a>() calls so that incremental rendering is possible. With QGLWidget (and naturally <a href="../qtgui/qopenglwindow.html">QOpenGLWindow</a> with the default update behavior) this is usually not the case because swapping the buffers leaves the back buffer with undefined contents.</p>
<p><b>Note: </b>Most applications do not need incremental rendering because they will render everything in the view on every paint call. In this case it is important to call glClear() as early as possible in <a href="qopenglwidget.html#paintGL">paintGL</a>(). This helps mobile GPUs that use a tile-based architecture to recognize that the tile buffer does not need to be reloaded with the framebuffer's previous contents. Omitting the clear call can lead to significant performance drops on such systems.</p><p><b>Note: </b>Avoid calling <a href="qwidget.html#winId">winId</a>() on a <a href="qopenglwidget.html">QOpenGLWidget</a>. This function triggers the creation of a native window, resulting in reduced performance and possibly rendering glitches.</p><a name="differences-to-qglwidget"></a>
<h3 >Differences to QGLWidget</h3>
<p>Besides the main conceptual difference of being backed by a framebuffer object, there are a number of smaller, internal differences between <a href="qopenglwidget.html">QOpenGLWidget</a> and the older QGLWidget:</p>
<ul>
<li>OpenGL state when invoking <a href="qopenglwidget.html#paintGL">paintGL</a>(). <a href="qopenglwidget.html">QOpenGLWidget</a> sets up the viewport via glViewport(). It does not perform any clearing.</li>
<li>Clearing when starting to paint via <a href="../qtgui/qpainter.html">QPainter</a>. Unlike regular widgets, QGLWidget defaulted to a value of <code>true</code> for <a href="qwidget.html#autoFillBackground-prop">autoFillBackground</a>. It then performed clearing to the palette's background color every time <a href="../qtgui/qpainter.html#begin">QPainter::begin</a>() was used. <a href="qopenglwidget.html">QOpenGLWidget</a> does not follow this: <a href="qwidget.html#autoFillBackground-prop">autoFillBackground</a> defaults to false, like for any other widget. The only exception is when being used as a viewport for other widgets like <a href="qgraphicsview.html">QGraphicsView</a>. In such a case <a href="qwidget.html#autoFillBackground-prop">autoFillBackground</a> will be automatically set to true to ensure compatibility with QGLWidget-based viewports.</li>
</ul>
<a name="multisampling"></a>
<h3 >Multisampling</h3>
<p>To enable multisampling, set the number of requested samples on the <a href="../qtgui/qsurfaceformat.html">QSurfaceFormat</a> that is passed to <a href="qopenglwidget.html#setFormat">setFormat</a>(). On systems that do not support it the request may get ignored.</p>
<p>Multisampling support requires support for multisampled renderbuffers and framebuffer blits. On OpenGL ES 2.0 implementations it is likely that these will not be present. This means that multisampling will not be available. With modern OpenGL versions and OpenGL ES 3.0 and up this is usually not a problem anymore.</p>
<a name="threading"></a>
<h3 >Threading</h3>
<p>Performing offscreen rendering on worker threads, for example to generate textures that are then used in the GUI/main thread in <a href="qopenglwidget.html#paintGL">paintGL</a>(), are supported by exposing the widget's <a href="../qtgui/qopenglcontext.html">QOpenGLContext</a> so that additional contexts sharing with it can be created on each thread.</p>
<p>Drawing directly to the <a href="qopenglwidget.html">QOpenGLWidget</a>'s framebuffer outside the GUI/main thread is possible by reimplementing <a href="qopenglwidget.html#paintEvent">paintEvent</a>() to do nothing. The context's thread affinity has to be changed via <a href="../qtcore/qobject.html#moveToThread">QObject::moveToThread</a>(). After that, <a href="qopenglwidget.html#makeCurrent">makeCurrent</a>() and <a href="qopenglwidget.html#doneCurrent">doneCurrent</a>() are usable on the worker thread. Be careful to move the context back to the GUI/main thread afterwards.</p>
<p>Unlike QGLWidget, triggering a buffer swap just for the <a href="qopenglwidget.html">QOpenGLWidget</a> is not possible since there is no real, onscreen native surface for it. Instead, it is up to the widget stack to manage composition and buffer swaps on the gui thread. When a thread is done updating the framebuffer, call <a href="qwidget.html#update">update</a>() <b>on the GUI/main thread</b> to schedule composition.</p>
<p>Extra care has to be taken to avoid using the framebuffer when the GUI/main thread is performing compositing. The signals <a href="qopenglwidget.html#aboutToCompose">aboutToCompose</a>() and <a href="qopenglwidget.html#frameSwapped">frameSwapped</a>() will be emitted when the composition is starting and ending. They are emitted on the GUI/main thread. This means that by using a direct connection <a href="qopenglwidget.html#aboutToCompose">aboutToCompose</a>() can block the GUI/main thread until the worker thread has finished its rendering. After that, the worker thread must perform no further rendering until the <a href="qopenglwidget.html#frameSwapped">frameSwapped</a>() signal is emitted. If this is not acceptable, the worker thread has to implement a double buffering mechanism. This involves drawing using an alternative render target, that is fully controlled by the thread, e.g&#x2e; an additional framebuffer object, and blitting to the <a href="qopenglwidget.html">QOpenGLWidget</a>'s framebuffer at a suitable time.</p>
<a name="context-sharing"></a>
<h3 >Context Sharing</h3>
<p>When multiple QOpenGLWidgets are added as children to the same top-level widget, their contexts will share with each other. This does not apply for <a href="qopenglwidget.html">QOpenGLWidget</a> instances that belong to different windows.</p>
<p>This means that all QOpenGLWidgets in the same window can access each other's sharable resources, like textures, and there is no need for an extra &quot;global share&quot; context, as was the case with QGLWidget.</p>
<p>To set up sharing between <a href="qopenglwidget.html">QOpenGLWidget</a> instances belonging to different windows, set the <a href="../qtcore/qt.html#ApplicationAttribute-enum">Qt::AA_ShareOpenGLContexts</a> application attribute before instantiating <a href="qapplication.html">QApplication</a>. This will trigger sharing between all <a href="qopenglwidget.html">QOpenGLWidget</a> instances without any further steps.</p>
<p>Creating extra <a href="../qtgui/qopenglcontext.html">QOpenGLContext</a> instances that share resources like textures with the <a href="qopenglwidget.html">QOpenGLWidget</a>'s context is also possible. Simply pass the pointer returned from <a href="qopenglwidget.html#context">context</a>() to <a href="../qtgui/qopenglcontext.html#setShareContext">QOpenGLContext::setShareContext</a>() before calling <a href="../qtgui/qopenglcontext.html#create">QOpenGLContext::create</a>(). The resulting context can also be used on a different thread, allowing threaded generation of textures and asynchronous texture uploads.</p>
<p>Note that <a href="qopenglwidget.html">QOpenGLWidget</a> expects a standard conformant implementation of resource sharing when it comes to the underlying graphics drivers. For example, some drivers, in particular for mobile and embedded hardware, have issues with setting up sharing between an existing context and others that are created later. Some other drivers may behave in unexpected ways when trying to utilize shared resources between different threads.</p>
<a name="resource-initialization-and-cleanup"></a>
<h3 >Resource Initialization and Cleanup</h3>
<p>The <a href="qopenglwidget.html">QOpenGLWidget</a>'s associated OpenGL context is guaranteed to be current whenever <a href="qopenglwidget.html#initializeGL">initializeGL</a>() and <a href="qopenglwidget.html#paintGL">paintGL</a>() are invoked. Do not attempt to create OpenGL resources before <a href="qopenglwidget.html#initializeGL">initializeGL</a>() is called. For example, attempting to compile shaders, initialize vertex buffer objects or upload texture data will fail when done in a subclass's constructor. These operations must be deferred to <a href="qopenglwidget.html#initializeGL">initializeGL</a>(). Some of Qt's OpenGL helper classes, like <a href="../qtgui/qopenglbuffer.html">QOpenGLBuffer</a> or <a href="../qtgui/qopenglvertexarrayobject.html">QOpenGLVertexArrayObject</a>, have a matching deferred behavior: they can be instantiated without a context, but all initialization is deferred until a <a href="qwidget.html#create">create</a>(), or similar, call. This means that they can be used as normal (non-pointer) member variables in a <a href="qopenglwidget.html">QOpenGLWidget</a> subclass, but the <a href="qwidget.html#create">create</a>() or similar function can only be called from <a href="qopenglwidget.html#initializeGL">initializeGL</a>(). Be aware however that not all classes are designed like this. When in doubt, make the member variable a pointer and create and destroy the instance dynamically in <a href="qopenglwidget.html#initializeGL">initializeGL</a>() and the destructor, respectively.</p>
<p>Releasing the resources also needs the context to be current. Therefore destructors that perform such cleanup are expected to call <a href="qopenglwidget.html#makeCurrent">makeCurrent</a>() before moving on to destroy any OpenGL resources or wrappers. Avoid deferred deletion via <a href="../qtcore/qobject.html#deleteLater">deleteLater()</a> or the parenting mechanism of <a href="../qtcore/qobject.html">QObject</a>. There is no guarantee the correct context will be current at the time the instance in question is really destroyed.</p>
<p>A typical subclass will therefore often look like the following when it comes to resource initialization and destruction:</p>
<pre class="cpp">

  <span class="keyword">class</span> MyGLWidget : <span class="keyword">public</span> <span class="type"><a href="qopenglwidget.html#QOpenGLWidget">QOpenGLWidget</a></span>
  {
      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>

  <span class="keyword">private</span>:
      <span class="type"><a href="../qtgui/qopenglvertexarrayobject.html">QOpenGLVertexArrayObject</a></span> m_vao;
      <span class="type"><a href="../qtgui/qopenglbuffer.html">QOpenGLBuffer</a></span> m_vbo;
      <span class="type"><a href="../qtgui/qopenglshaderprogram.html">QOpenGLShaderProgram</a></span> <span class="operator">*</span>m_program;
      <span class="type"><a href="../qtgui/qopenglshader.html">QOpenGLShader</a></span> <span class="operator">*</span>m_shader;
      <span class="type"><a href="../qtgui/qopengltexture.html">QOpenGLTexture</a></span> <span class="operator">*</span>m_texture;
  };

  MyGLWidget<span class="operator">::</span>MyGLWidget()
      : m_program(<span class="number">0</span>)<span class="operator">,</span> m_shader(<span class="number">0</span>)<span class="operator">,</span> m_texture(<span class="number">0</span>)
  {
      <span class="comment">// No OpenGL resource initialization is done here.</span>
  }

  MyGLWidget<span class="operator">::</span><span class="operator">~</span>MyGLWidget()
  {
      <span class="comment">// Make sure the context is current and then explicitly</span>
      <span class="comment">// destroy all underlying OpenGL resources.</span>
      makeCurrent();

      <span class="keyword">delete</span> m_texture;
      <span class="keyword">delete</span> m_shader;
      <span class="keyword">delete</span> m_program;

      m_vbo<span class="operator">.</span>destroy();
      m_vao<span class="operator">.</span>destroy();

      doneCurrent();
  }

  <span class="type">void</span> MyGLWidget<span class="operator">::</span>initializeGL()
  {
      m_vao<span class="operator">.</span>create();
      <span class="keyword">if</span> (m_vao<span class="operator">.</span>isCreated())
          m_vao<span class="operator">.</span>bind();

      m_vbo<span class="operator">.</span>create();
      m_vbo<span class="operator">.</span>bind();
      m_vbo<span class="operator">.</span>allocate(<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>);

      m_texture <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtgui/qopengltexture.html">QOpenGLTexture</a></span>(<span class="type"><a href="../qtgui/qimage.html">QImage</a></span>(<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>));

      m_shader <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtgui/qopenglshader.html">QOpenGLShader</a></span>(<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>);
      m_program <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtgui/qopenglshaderprogram.html">QOpenGLShaderProgram</a></span>(<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>);

      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
  }

</pre>
<p>This is naturally not the only possible solution. One alternative is to use the <a href="../qtgui/qopenglcontext.html#aboutToBeDestroyed">aboutToBeDestroyed()</a> signal of <a href="../qtgui/qopenglcontext.html">QOpenGLContext</a>. By connecting a slot, using direct connection, to this signal, it is possible to perform cleanup whenever the the underlying native context handle, or the entire <a href="../qtgui/qopenglcontext.html">QOpenGLContext</a> instance, is going to be released. The following snippet is in principal equivalent to the previous one:</p>
<pre class="cpp">

  <span class="type">void</span> MyGLWidget<span class="operator">::</span>initializeGL()
  {
      <span class="comment">// context() and QOpenGLContext::currentContext() are equivalent when called from initializeGL or paintGL.</span>
      connect(context()<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="../qtgui/qopenglcontext.html">QOpenGLContext</a></span><span class="operator">::</span>aboutToBeDestroyed<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MyGLWidget<span class="operator">::</span>cleanup);
  }

  <span class="type">void</span> MyGLWidget<span class="operator">::</span>cleanup()
  {
      makeCurrent();
      <span class="keyword">delete</span> m_texture;
      m_texture <span class="operator">=</span> <span class="number">0</span>;
      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
      doneCurrent();
  }

</pre>
<p><b>Note: </b>For widgets that change their associated top-level window multiple times during their lifetime, a combined approach is essential. Whenever the widget or a parent of it gets reparented so that the top-level window becomes different, the widget's associated context is destroyed and a new one is created. This is then followed by a call to <a href="qopenglwidget.html#initializeGL">initializeGL</a>() where all OpenGL resources must get reinitialized. Due to this the only option to perform proper cleanup is to connect to the context's aboutToBeDestroyed() signal. Note that the context in question may not be the current one when the signal gets emitted. Therefore it is good practice to call <a href="qopenglwidget.html#makeCurrent">makeCurrent</a>() in the connected slot. Additionally, the same cleanup steps must be performed from the derived class' destructor, since the slot connected to the signal will not get invoked when the widget is being destroyed.</p><p><b>Note: </b>When <a href="../qtcore/qt.html#ApplicationAttribute-enum">Qt::AA_ShareOpenGLContexts</a> is set, the widget's context never changes, not even when reparenting because the widget's associated texture is guaranteed to be accessible also from the new top-level's context.</p><p>Proper cleanup is especially important due to context sharing. Even though each <a href="qopenglwidget.html">QOpenGLWidget</a>'s associated context is destroyed together with the <a href="qopenglwidget.html">QOpenGLWidget</a>, the sharable resources in that context, like textures, will stay valid until the top-level window, in which the <a href="qopenglwidget.html">QOpenGLWidget</a> lived, is destroyed. Additionally, settings like <a href="../qtcore/qt.html#ApplicationAttribute-enum">Qt::AA_ShareOpenGLContexts</a> and some Qt modules may trigger an even wider scope for sharing contexts, potentially leading to keeping the resources in question alive for the entire lifetime of the application. Therefore the safest and most robust is always to perform explicit cleanup for all resources and resource wrappers used in the <a href="qopenglwidget.html">QOpenGLWidget</a>.</p>
<a name="limitations"></a>
<h3 >Limitations</h3>
<p>Putting other widgets underneath and making the <a href="qopenglwidget.html">QOpenGLWidget</a> transparent will not lead to the expected results: The widgets underneath will not be visible. This is because in practice the <a href="qopenglwidget.html">QOpenGLWidget</a> is drawn before all other regular, non-OpenGL widgets, and so see-through type of solutions are not feasible. Other type of layouts, like having widgets on top of the <a href="qopenglwidget.html">QOpenGLWidget</a>, will function as expected.</p>
<p>When absolutely necessary, this limitation can be overcome by setting the <a href="../qtcore/qt.html#WidgetAttribute-enum">Qt::WA_AlwaysStackOnTop</a> attribute on the <a href="qopenglwidget.html">QOpenGLWidget</a>. Be aware however that this breaks stacking order, for example it will not be possible to have other widgets on top of the <a href="qopenglwidget.html">QOpenGLWidget</a>, so it should only be used in situations where a semi-transparent <a href="qopenglwidget.html">QOpenGLWidget</a> with other widgets visible underneath is required.</p>
<p>Note that this does not apply when there are no other widgets underneath and the intention is to have a semi-transparent window. In that case the traditional approach of setting <a href="../qtcore/qt.html#WidgetAttribute-enum">Qt::WA_TranslucentBackground</a> on the top-level window is sufficient. Note that if the transparent areas are only desired in the <a href="qopenglwidget.html">QOpenGLWidget</a>, then <a href="../qtcore/qt.html#WidgetAttribute-enum">Qt::WA_NoSystemBackground</a> will need to be turned back to <code>false</code> after enabling <a href="../qtcore/qt.html#WidgetAttribute-enum">Qt::WA_TranslucentBackground</a>. Additionally, requesting an alpha channel for the <a href="qopenglwidget.html">QOpenGLWidget</a>'s context via <a href="qopenglwidget.html#setFormat">setFormat</a>() may be necessary too, depending on the system.</p>
<p><a href="qopenglwidget.html">QOpenGLWidget</a> supports multiple update behaviors, just like <a href="../qtgui/qopenglwindow.html">QOpenGLWindow</a>. In preserved mode the rendered content from the previous <a href="qopenglwidget.html#paintGL">paintGL</a>() call is available in the next one, allowing incremental rendering. In non-preserved mode the content is lost and <a href="qopenglwidget.html#paintGL">paintGL</a>() implementations are expected to redraw everything in the view.</p>
<p>Before Qt 5.5 the default behavior of <a href="qopenglwidget.html">QOpenGLWidget</a> was to preserve the rendered contents between <a href="qopenglwidget.html#paintGL">paintGL</a>() calls. Since Qt 5.5 the default behavior is non-preserved because this provides better performance and the majority of applications have no need for the previous content. This also resembles the semantics of an OpenGL-based <a href="../qtgui/qwindow.html">QWindow</a> and matches the default behavior of <a href="../qtgui/qopenglwindow.html">QOpenGLWindow</a> in that the color and ancillary buffers are invalidated for each frame. To restore the preserved behavior, call <a href="qopenglwidget.html#setUpdateBehavior">setUpdateBehavior</a>() with <code>PartialUpdate</code>.</p>
<a name="alternatives"></a>
<h3 >Alternatives</h3>
<p>Adding a <a href="qopenglwidget.html">QOpenGLWidget</a> into a window turns on OpenGL-based compositing for the entire window. In some special cases this may not be ideal, and the old QGLWidget-style behavior with a separate, native child window is desired. Desktop applications that understand the limitations of this approach (for example when it comes to overlaps, transparency, scroll views and MDI areas), can use <a href="../qtgui/qopenglwindow.html">QOpenGLWindow</a> with <a href="qwidget.html#createWindowContainer">QWidget::createWindowContainer</a>(). This is a modern alternative to QGLWidget and is faster than <a href="qopenglwidget.html">QOpenGLWidget</a> due to the lack of the additional composition step. It is strongly recommended to limit the usage of this approach to cases where there is no other choice. Note that this option is not suitable for most embedded and mobile platforms, and it is known to have issues on certain desktop platforms (e.g&#x2e; macOS) too. The stable, cross-platform solution is always <a href="qopenglwidget.html">QOpenGLWidget</a>.</p>
<p><i>OpenGL is a trademark of Silicon Graphics, Inc. in the United States and other countries.</i></p>
</div>
<p><b>See also </b><a href="../qtgui/qopenglfunctions.html">QOpenGLFunctions</a>, <a href="../qtgui/qopenglwindow.html">QOpenGLWindow</a>, <a href="../qtcore/qt.html#ApplicationAttribute-enum">Qt::AA_ShareOpenGLContexts</a>, and <a href="qopenglwidget.html#UpdateBehavior-enum">UpdateBehavior</a>.</p>
<!-- @@@QOpenGLWidget -->
<div class="types">
<h2>Member Type Documentation</h2>
<!-- $$$UpdateBehavior$$$NoPartialUpdate$$$PartialUpdate -->
<h3 class="fn" id="UpdateBehavior-enum"><a name="UpdateBehavior-enum"></a>enum QOpenGLWidget::<span class="name">UpdateBehavior</span></h3>
<p>This enum describes the update semantics of <a href="qopenglwidget.html">QOpenGLWidget</a>.</p>
<div class="table"><table class="valuelist"><tr valign="top" class="odd"><th class="tblConst">Constant</th><th class="tblval">Value</th><th class="tbldscr">Description</th></tr>
<tr><td class="topAlign"><code>QOpenGLWidget::NoPartialUpdate</code></td><td class="topAlign tblval"><code>0</code></td><td class="topAlign"><a href="qopenglwidget.html">QOpenGLWidget</a> will discard the contents of the color buffer and the ancillary buffers after the <a href="qopenglwidget.html">QOpenGLWidget</a> is rendered to screen. This is the same behavior that can be expected by calling <a href="../qtgui/qopenglcontext.html#swapBuffers">QOpenGLContext::swapBuffers</a> with a default opengl enabled <a href="../qtgui/qwindow.html">QWindow</a> as the argument. NoPartialUpdate can have some performance benefits on certain hardware architectures common in the mobile and embedded space when a framebuffer object is used as the rendering target. The framebuffer object is invalidated between frames with glDiscardFramebufferEXT if supported or a glClear. Please see the documentation of EXT_discard_framebuffer for more information: https://www.khronos.org/registry/gles/extensions/EXT/EXT_discard_framebuffer.txt</td></tr>
<tr><td class="topAlign"><code>QOpenGLWidget::PartialUpdate</code></td><td class="topAlign tblval"><code>1</code></td><td class="topAlign">The framebuffer objects color buffer and ancillary buffers are not invalidated between frames.</td></tr>
</table></div>
<p>This enum was introduced or modified in  Qt 5.5.</p>
<p><b>See also </b><a href="qopenglwidget.html#updateBehavior">updateBehavior</a>() and <a href="qopenglwidget.html#setUpdateBehavior">setUpdateBehavior</a>().</p>
<!-- @@@UpdateBehavior -->
</div>
<div class="func">
<h2>Member Function Documentation</h2>
<!-- $$$QOpenGLWidget[overload1]$$$QOpenGLWidgetQWidget*Qt::WindowFlags -->
<h3 class="fn" id="QOpenGLWidget"><a name="QOpenGLWidget"></a>QOpenGLWidget::<span class="name">QOpenGLWidget</span>(<span class="type"><a href="qwidget.html#QWidget">QWidget</a></span> *<i>parent</i> = Q_NULLPTR, <span class="type"><a href="../qtcore/qt.html#WindowType-enum">Qt::WindowFlags</a></span> <i>f</i> = Qt::WindowFlags())</h3>
<p>Constructs a widget which is a child of <i>parent</i>, with widget flags set to <i>f</i>.</p>
<!-- @@@QOpenGLWidget -->
<!-- $$$~QOpenGLWidget[overload1]$$$~QOpenGLWidget -->
<h3 class="fn" id="dtor.QOpenGLWidget"><a name="dtor.QOpenGLWidget"></a>QOpenGLWidget::<span class="name">~QOpenGLWidget</span>()</h3>
<p>Destroys the <a href="qopenglwidget.html">QOpenGLWidget</a> instance, freeing its resources.</p>
<p>The <a href="qopenglwidget.html">QOpenGLWidget</a>'s context is made current in the destructor, allowing for safe destruction of any child object that may need to release OpenGL resources belonging to the context provided by this widget.</p>
<p><b>Warning:</b> if you have objects wrapping OpenGL resources (such as <a href="../qtgui/qopenglbuffer.html">QOpenGLBuffer</a>, <a href="../qtgui/qopenglshaderprogram.html">QOpenGLShaderProgram</a>, etc.) as members of a OpenGLWidget subclass, you may need to add a call to <a href="qopenglwidget.html#makeCurrent">makeCurrent</a>() in that subclass' destructor as well. Due to the rules of C++ object destruction, those objects will be destroyed <i>before</i> calling this function (but after that the destructor of the subclass has run), therefore making the OpenGL context current in this function happens too late for their safe disposal.</p>
<p><b>See also </b><a href="qopenglwidget.html#makeCurrent">makeCurrent</a>.</p>
<!-- @@@~QOpenGLWidget -->
<!-- $$$aboutToCompose[overload1]$$$aboutToCompose -->
<h3 class="fn" id="aboutToCompose"><a name="aboutToCompose"></a><code>[signal] </code><span class="type">void</span> QOpenGLWidget::<span class="name">aboutToCompose</span>()</h3>
<p>This signal is emitted when the widget's top-level window is about to begin composing the textures of its <a href="qopenglwidget.html">QOpenGLWidget</a> children and the other widgets.</p>
<!-- @@@aboutToCompose -->
<!-- $$$aboutToResize[overload1]$$$aboutToResize -->
<h3 class="fn" id="aboutToResize"><a name="aboutToResize"></a><code>[signal] </code><span class="type">void</span> QOpenGLWidget::<span class="name">aboutToResize</span>()</h3>
<p>This signal is emitted when the widget's size is changed and therefore the framebuffer object is going to be recreated.</p>
<!-- @@@aboutToResize -->
<!-- $$$context[overload1]$$$context -->
<h3 class="fn" id="context"><a name="context"></a><span class="type"><a href="../qtgui/qopenglcontext.html">QOpenGLContext</a></span> *QOpenGLWidget::<span class="name">context</span>() const</h3>
<p>Returns The <a href="../qtgui/qopenglcontext.html">QOpenGLContext</a> used by this widget or <code>0</code> if not yet initialized.</p>
<p><b>Note: </b>The context and the framebuffer object used by the widget changes when reparenting the widget via <a href="qwidget.html#setParent">setParent</a>().</p><p><b>See also </b><a href="../qtgui/qopenglcontext.html#setShareContext">QOpenGLContext::setShareContext</a>() and <a href="qopenglwidget.html#defaultFramebufferObject">defaultFramebufferObject</a>().</p>
<!-- @@@context -->
<!-- $$$defaultFramebufferObject[overload1]$$$defaultFramebufferObject -->
<h3 class="fn" id="defaultFramebufferObject"><a name="defaultFramebufferObject"></a><span class="type">GLuint</span> QOpenGLWidget::<span class="name">defaultFramebufferObject</span>() const</h3>
<p>Returns The framebuffer object handle or <code>0</code> if not yet initialized.</p>
<p><b>Note: </b>The framebuffer object belongs to the context returned by <a href="qopenglwidget.html#context">context</a>() and may not be accessible from other contexts.</p><p><b>Note: </b>The context and the framebuffer object used by the widget changes when reparenting the widget via <a href="qwidget.html#setParent">setParent</a>(). In addition, the framebuffer object changes on each resize.</p><p><b>See also </b><a href="qopenglwidget.html#context">context</a>().</p>
<!-- @@@defaultFramebufferObject -->
<!-- $$$doneCurrent[overload1]$$$doneCurrent -->
<h3 class="fn" id="doneCurrent"><a name="doneCurrent"></a><span class="type">void</span> QOpenGLWidget::<span class="name">doneCurrent</span>()</h3>
<p>Releases the context.</p>
<p>It is not necessary to call this function in most cases, since the widget will make sure the context is bound and released properly when invoking <a href="qopenglwidget.html#paintGL">paintGL</a>().</p>
<!-- @@@doneCurrent -->
<!-- $$$format[overload1]$$$format -->
<h3 class="fn" id="format"><a name="format"></a><span class="type"><a href="../qtgui/qsurfaceformat.html">QSurfaceFormat</a></span> QOpenGLWidget::<span class="name">format</span>() const</h3>
<p>Returns the context and surface format used by this widget and its toplevel window.</p>
<p>After the widget and its toplevel have both been created, resized and shown, this function will return the actual format of the context. This may differ from the requested format if the request could not be fulfilled by the platform. It is also possible to get larger color buffer sizes than requested.</p>
<p>When the widget's window and the related OpenGL resources are not yet initialized, the return value is the format that has been set via <a href="qopenglwidget.html#setFormat">setFormat</a>().</p>
<p><b>See also </b><a href="qopenglwidget.html#setFormat">setFormat</a>() and <a href="qopenglwidget.html#context">context</a>().</p>
<!-- @@@format -->
<!-- $$$frameSwapped[overload1]$$$frameSwapped -->
<h3 class="fn" id="frameSwapped"><a name="frameSwapped"></a><code>[signal] </code><span class="type">void</span> QOpenGLWidget::<span class="name">frameSwapped</span>()</h3>
<p>This signal is emitted after the widget's top-level window has finished composition and returned from its potentially blocking <a href="../qtgui/qopenglcontext.html#swapBuffers">QOpenGLContext::swapBuffers</a>() call.</p>
<!-- @@@frameSwapped -->
<!-- $$$grabFramebuffer[overload1]$$$grabFramebuffer -->
<h3 class="fn" id="grabFramebuffer"><a name="grabFramebuffer"></a><span class="type"><a href="../qtgui/qimage.html">QImage</a></span> QOpenGLWidget::<span class="name">grabFramebuffer</span>()</h3>
<p>Renders and returns a 32-bit RGB image of the framebuffer.</p>
<p><b>Note: </b>This is a potentially expensive operation because it relies on glReadPixels() to read back the pixels. This may be slow and can stall the GPU pipeline.</p><!-- @@@grabFramebuffer -->
<!-- $$$initializeGL[overload1]$$$initializeGL -->
<h3 class="fn" id="initializeGL"><a name="initializeGL"></a><code>[virtual protected] </code><span class="type">void</span> QOpenGLWidget::<span class="name">initializeGL</span>()</h3>
<p>This virtual function is called once before the first call to <a href="qopenglwidget.html#paintGL">paintGL</a>() or <a href="qopenglwidget.html#resizeGL">resizeGL</a>(). Reimplement it in a subclass.</p>
<p>This function should set up any required OpenGL resources and state.</p>
<p>There is no need to call <a href="qopenglwidget.html#makeCurrent">makeCurrent</a>() because this has already been done when this function is called. Note however that the framebuffer is not yet available at this stage, so avoid issuing draw calls from here. Defer such calls to <a href="qopenglwidget.html#paintGL">paintGL</a>() instead.</p>
<p><b>See also </b><a href="qopenglwidget.html#paintGL">paintGL</a>() and <a href="qopenglwidget.html#resizeGL">resizeGL</a>().</p>
<!-- @@@initializeGL -->
<!-- $$$isValid[overload1]$$$isValid -->
<h3 class="fn" id="isValid"><a name="isValid"></a><span class="type">bool</span> QOpenGLWidget::<span class="name">isValid</span>() const</h3>
<p>Returns <i>true</i> if the widget and OpenGL resources, like the context, have been successfully initialized. Note that the return value is always false until the widget is shown.</p>
<!-- @@@isValid -->
<!-- $$$makeCurrent[overload1]$$$makeCurrent -->
<h3 class="fn" id="makeCurrent"><a name="makeCurrent"></a><span class="type">void</span> QOpenGLWidget::<span class="name">makeCurrent</span>()</h3>
<p>Prepares for rendering OpenGL content for this widget by making the corresponding context current and binding the framebuffer object in that context.</p>
<p>It is not necessary to call this function in most cases, because it is called automatically before invoking <a href="qopenglwidget.html#paintGL">paintGL</a>().</p>
<p><b>See also </b><a href="qopenglwidget.html#context">context</a>(), <a href="qopenglwidget.html#paintGL">paintGL</a>(), and <a href="qopenglwidget.html#doneCurrent">doneCurrent</a>().</p>
<!-- @@@makeCurrent -->
<!-- $$$paintEvent[overload1]$$$paintEventQPaintEvent* -->
<h3 class="fn" id="paintEvent"><a name="paintEvent"></a><code>[virtual protected] </code><span class="type">void</span> QOpenGLWidget::<span class="name">paintEvent</span>(<span class="type"><a href="../qtgui/qpaintevent.html">QPaintEvent</a></span> *<i>e</i>)</h3>
<p>Reimplemented from <a href="qwidget.html#paintEvent">QWidget::paintEvent</a>().</p>
<p>Handles paint events.</p>
<p>Calling <a href="qwidget.html#update">QWidget::update</a>() will lead to sending a paint event <i>e</i>, and thus invoking this function. (NB this is asynchronous and will happen at some point after returning from <a href="qwidget.html#update">update</a>()). This function will then, after some preparation, call the virtual <a href="qopenglwidget.html#paintGL">paintGL</a>() to update the contents of the <a href="qopenglwidget.html">QOpenGLWidget</a>'s framebuffer. The widget's top-level window will then composite the framebuffer's texture with the rest of the window.</p>
<!-- @@@paintEvent -->
<!-- $$$paintGL[overload1]$$$paintGL -->
<h3 class="fn" id="paintGL"><a name="paintGL"></a><code>[virtual protected] </code><span class="type">void</span> QOpenGLWidget::<span class="name">paintGL</span>()</h3>
<p>This virtual function is called whenever the widget needs to be painted. Reimplement it in a subclass.</p>
<p>There is no need to call <a href="qopenglwidget.html#makeCurrent">makeCurrent</a>() because this has already been done when this function is called.</p>
<p>Before invoking this function, the context and the framebuffer are bound, and the viewport is set up by a call to glViewport(). No other state is set and no clearing or drawing is performed by the framework.</p>
<p><b>See also </b><a href="qopenglwidget.html#initializeGL">initializeGL</a>() and <a href="qopenglwidget.html#resizeGL">resizeGL</a>().</p>
<!-- @@@paintGL -->
<!-- $$$resizeEvent[overload1]$$$resizeEventQResizeEvent* -->
<h3 class="fn" id="resizeEvent"><a name="resizeEvent"></a><code>[virtual protected] </code><span class="type">void</span> QOpenGLWidget::<span class="name">resizeEvent</span>(<span class="type"><a href="../qtgui/qresizeevent.html">QResizeEvent</a></span> *<i>e</i>)</h3>
<p>Reimplemented from <a href="qwidget.html#resizeEvent">QWidget::resizeEvent</a>().</p>
<p>Handles resize events that are passed in the <i>e</i> event parameter. Calls the virtual function <a href="qopenglwidget.html#resizeGL">resizeGL</a>().</p>
<p><b>Note: </b>Avoid overriding this function in derived classes. If that is not feasible, make sure that <a href="qopenglwidget.html">QOpenGLWidget</a>'s implementation is invoked too. Otherwise the underlying framebuffer object and related resources will not get resized properly and will lead to incorrect rendering.</p><!-- @@@resizeEvent -->
<!-- $$$resizeGL[overload1]$$$resizeGLintint -->
<h3 class="fn" id="resizeGL"><a name="resizeGL"></a><code>[virtual protected] </code><span class="type">void</span> QOpenGLWidget::<span class="name">resizeGL</span>(<span class="type">int</span> <i>w</i>, <span class="type">int</span> <i>h</i>)</h3>
<p>This virtual function is called whenever the widget has been resized. Reimplement it in a subclass. The new size is passed in <i>w</i> and <i>h</i>.</p>
<p>There is no need to call <a href="qopenglwidget.html#makeCurrent">makeCurrent</a>() because this has already been done when this function is called. Additionally, the framebuffer is also bound.</p>
<p><b>See also </b><a href="qopenglwidget.html#initializeGL">initializeGL</a>() and <a href="qopenglwidget.html#paintGL">paintGL</a>().</p>
<!-- @@@resizeGL -->
<!-- $$$resized[overload1]$$$resized -->
<h3 class="fn" id="resized"><a name="resized"></a><code>[signal] </code><span class="type">void</span> QOpenGLWidget::<span class="name">resized</span>()</h3>
<p>This signal is emitted right after the framebuffer object has been recreated due to resizing the widget.</p>
<!-- @@@resized -->
<!-- $$$setFormat[overload1]$$$setFormatconstQSurfaceFormat& -->
<h3 class="fn" id="setFormat"><a name="setFormat"></a><span class="type">void</span> QOpenGLWidget::<span class="name">setFormat</span>(const <span class="type"><a href="../qtgui/qsurfaceformat.html">QSurfaceFormat</a></span> &amp;<i>format</i>)</h3>
<p>Sets the requested surface <i>format</i>.</p>
<p>When the format is not explicitly set via this function, the format returned by <a href="../qtgui/qsurfaceformat.html#defaultFormat">QSurfaceFormat::defaultFormat</a>() will be used. This means that when having multiple OpenGL widgets, individual calls to this function can be replaced by one single call to <a href="../qtgui/qsurfaceformat.html#setDefaultFormat">QSurfaceFormat::setDefaultFormat</a>() before creating the first widget.</p>
<p><b>Note: </b>Requesting an alpha buffer via this function will not lead to the desired results when the intention is to make other widgets beneath visible. Instead, use <a href="../qtcore/qt.html#WidgetAttribute-enum">Qt::WA_AlwaysStackOnTop</a> to enable semi-transparent <a href="qopenglwidget.html">QOpenGLWidget</a> instances with other widgets visible underneath. Keep in mind however that this breaks the stacking order, so it will no longer be possible to have other widgets on top of the <a href="qopenglwidget.html">QOpenGLWidget</a>.</p><p><b>See also </b><a href="qopenglwidget.html#format">format</a>(), <a href="../qtcore/qt.html#WidgetAttribute-enum">Qt::WA_AlwaysStackOnTop</a>, and <a href="../qtgui/qsurfaceformat.html#setDefaultFormat">QSurfaceFormat::setDefaultFormat</a>().</p>
<!-- @@@setFormat -->
<!-- $$$setUpdateBehavior[overload1]$$$setUpdateBehaviorUpdateBehavior -->
<h3 class="fn" id="setUpdateBehavior"><a name="setUpdateBehavior"></a><span class="type">void</span> QOpenGLWidget::<span class="name">setUpdateBehavior</span>(<span class="type"><a href="qopenglwidget.html#UpdateBehavior-enum">UpdateBehavior</a></span> <i>updateBehavior</i>)</h3>
<p>Sets this widget's update behavior to <i>updateBehavior</i>.</p>
<p>This function was introduced in  Qt 5.5.</p>
<p><b>See also </b><a href="qopenglwidget.html#updateBehavior">updateBehavior</a>().</p>
<!-- @@@setUpdateBehavior -->
<!-- $$$updateBehavior[overload1]$$$updateBehavior -->
<h3 class="fn" id="updateBehavior"><a name="updateBehavior"></a><span class="type"><a href="qopenglwidget.html#UpdateBehavior-enum">UpdateBehavior</a></span> QOpenGLWidget::<span class="name">updateBehavior</span>() const</h3>
<p>Returns the update behavior of the widget.</p>
<p>This function was introduced in  Qt 5.5.</p>
<p><b>See also </b><a href="qopenglwidget.html#setUpdateBehavior">setUpdateBehavior</a>().</p>
<!-- @@@updateBehavior -->
</div>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2017 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>