Cannot read GL_DEPTH_COMPONENT using QOpenGLWidget
<p>I am implementing a simple click selection for 3D objects inside a QOpenGLWidget. To do this I need to transform 2D mouse coordinates into 3D worldspace. I had previously implemented the whole thing using QGLWidget. Using QOpenGLWidget, I am not able to read the GL_DEPTH_COMPONENT of a pixel:</p>

<pre><code>float z;
glReadPixels(pixel.x, height - pixel.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &amp;z);
</code></pre>

<p>'z' is always 0.
To make sure my pixel coordinates were correct I tried receiving GL_RGBA values:</p>

<pre><code>float rgba[4];
glReadPixels((int)p_temp.x(), (int) (viewport[3] - p_temp.y()), 1, 1, GL_RGBA, GL_FLOAT, rgba);
</code></pre>

<p>, which returns the correct pixel Colors.
In order for this to work I had to change the domain of the pixel coordinates from local to parent coordinates. This probably results from the fact that the GL_VIEWPORT is set corresponding to the parent widgets size:</p>

<pre><code>int viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
</code></pre>

<p>for QGLWidget this returns: <code>{0, 0, this-&gt;width(), this-&gt;height()}</code></p>

<p>for QOpenGLWidget this returns: <code>{0, 0, this-&gt;parent()-&gt;width(), this-&gt;parent()-&gt;height()}</code></p>

<p>BTW, my OpenGL version is 4.5 and I do not get any OpenGL error using glReadPixels with GL_DEPTH_COMPONENT</p>

<p>Now, I am kind of clueless as to what I might be missing. Any ideas?</p>