How it works...

When creating a Qt Widgets Application project, try to do the following to improve performance:

  • Avoid adding too many pages to a stacked widget and filling them with widgets, as Qt needs to find all of them recursively during the rendering process and event handling, which will highly impact the program's performance.
  • Do note that the QWidget class uses the Raster Engine, a software render to render the widgets instead of using GPU. However, it is lightweight enough to keep the performance good most of the time. Alternatively, you should consider using QML for your program's GUI instead, since it is fully hardware accelerated.
  • Turn off mouseTracking, tabletTracking, and other event catching for your widgets if they do not need it. These trackings and catchings cost extra CPU usage to your program:

  • Keep your style sheets as simple as possible. A large style sheet needs a longer time for Qt to parse the information into the rendering system, which will also impact performance.
  • Different C++ containers produce different speeds, as we showed in the preceding example. Surprisingly, Qt's vector container is slightly slower than STL's (the C++ standard library) vector container. Overall, the good old C++ array is still the fastest, but does not provide sorting functionality. Use what is best for your requirements.
  • For large operations, use asynchronous methods whenever possible as it will not stall the main process and keep your program running smoothly.
  • Multi-threading is really good for running different operations in parallel event loops. However, it can also become quite ugly if not done right, for example, creating and destroying threads frequently, or inter-thread communications that are not planned well.
  • Try to avoid using the web engine unless absolutely necessary. This is because embedding a full web browser on your program is really heavy and overkill, especially for a small application. You can consider using QML instead of making a hybrid application if you want to create user interface-centric software.
  • By doing performance tests like we did in the preceding example project, you can easily determine which method is the best choice for your project and how to make your program perform better.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset