Game Programming using Qt 5 Beginner's Guide
上QQ阅读APP看书,第一时间看更新

OpenGL in the Graphics View

As a last optimization, you can take advantage of OpenGL. Instead of using the default viewport based on QWidget, advise Graphics View to use an OpenGL widget:

QGraphicsView view;
view.setViewport(new QOpenGLWidget()); 

This usually improves the rendering performance. However, Graphics View wasn't designed for GPUs and can't use them effectively. There are ways to improve the situation, but that goes beyond the topic and scope of this chapter. You can find more information about OpenGL and Graphics View in the Boxes Qt example as well as in R?dal's article "Accelerate your Widgets with OpenGL", which can be found online at https://doc.qt.io/archives/qq/qq26-openglcanvas.html.

If you want to use a framework designed to be GPU accelerated, you should turn your attention to Qt Quick (we will start working with it in Chapter 11, Introduction to Qt Quick). However, Qt Quick has its own limitations compared to Graphics View. This topic is elaborated in Nichols's article Should you still be using QGraphicsView?, available at https://blog.qt.io/blog/2017/01/19/should-you-be-using-qgraphicsview/. Alternatively, you can access the full power of OpenGL directly using its API and helpful Qt utilities. We will describe this approach in Chapter 9, OpenGL and Vulkan in Qt applications.

Unfortunately, we can't say that you have to do this or that to optimize Graphics View as it highly depends on your system and view/scene. What we can tell you, however, is how to proceed. Once you have finished your game based on Graphics View, measure the performance of your game using a profiler. Make an optimization you think may pay or simply guess, and then profile your game again. If the results are better, keep the change, otherwise reject it. This sounds simple and is the only way optimization can be done. There are no hidden tricks or deeper knowledge. With time, however, your forecasting will get better.