问题主要在标题中确定。我尝试了 Qt 的示例(2dpainting)并注意到,如果我尝试在 QGLWidget 上绘制,相同的代码会消耗更多的 CPU 功率,而如果我尝试在 QWidget 上简单地绘制则更少。我认为 QGLWidget 应该更快。还有一个更有趣的现象:在 QGLWidget 中,抗锯齿提示似乎被忽略了。
OpenGL 版本:3.3.0
那为什么呢?
问题主要在标题中确定。我尝试了 Qt 的示例(2dpainting)并注意到,如果我尝试在 QGLWidget 上绘制,相同的代码会消耗更多的 CPU 功率,而如果我尝试在 QWidget 上简单地绘制则更少。我认为 QGLWidget 应该更快。还有一个更有趣的现象:在 QGLWidget 中,抗锯齿提示似乎被忽略了。
OpenGL 版本:3.3.0
那为什么呢?
Firstly, note this text at the bottom of the documentation that you link to:
The example shows the same painting operations performed at the same time in a Widget and a GLWidget. The quality and speed of rendering in the GLWidget depends on the level of support for multisampling and hardware acceleration that your system's OpenGL driver provides. If support for either of these is lacking, the driver may fall back on a software renderer that may trade quality for speed.
Putting that aside, hardware rendering is not always guaranteed to be faster than software rendering; it all depends upon what the renderer is being asked to do.
An example of where software can exceed hardware is if the goal of the item being rendered is constantly changing. So, if you have a drawing program that draws a line being created by the mouse being constantly moved and it is implemented by adding points to a painter path that is drawn every frame, a hardware renderer will be subject to constant pipeline stalls as new points are added to the painter path. Setting up the graphics pipeline from a stall takes time, which is not something a software renderer has to deal with.
In the 2dPainting example you ask about, the helper class, which performs the paint calls, is doing a lot of unnecessary work; saving the painter state; setting the pen / brush; rotating the painter; restoring the brush. All of this is a bigger overhead in hardware than software. To really see hardware rendering outperform software, pre-calculating the objects' positions outside of the render loop (paint function) and then doing nothing put actually rendering in the paint function is likely to display a noticeable difference here.
Finally, regarding anti-aliasing, the documentation that you linked to states: "the QGLWidget will also use anti-aliasing if the required extensions are supported by your system's OpenGL driver"