如何优化基于 QGraphicsView 的应用程序的性能? [英] How can I optimize the performance of a QGraphicsView-based app?

查看:140
本文介绍了如何优化基于 QGraphicsView 的应用程序的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 Qt 图形视图框架的应用程序.
这是一个拼图游戏,它基本上将像素图切割成更小的像素图(拼图)并在 QGraphicsView 中将它们显示为 QGraphicsItem.我希望这个应用程序可以在智能手机和平板电脑上运行.(它已经在诺基亚 N900 和一些 Symbian 手机上运行.尚未针对 Symbian^3 进行优化.)
来源是Gitorious.

I have an app which is based on the Qt Graphics View framework.
It's a jigsaw puzzle game which basically cuts a pixmap into smaller pixmaps (puzzle pieces) and displays them as QGraphicsItems in a QGraphicsView. I want this app to run on smartphones and tablets. (It already runs on the Nokia N900 and some Symbian phones. Not optimized for Symbian^3 yet.)
The source is on Gitorious.

这些项目继承了QGraphicsItemQObject,并且具有pos()Q_PROPERTYQGraphicsItem 的 >rotation() 以启用使用 Qt 动画框架对其进行动画处理.
我对项目执行转换,例如缩放和旋转(后者仅在开发中的多点触控分支中),并且我还在它们上使用了 QGraphicsDropShadowEffect.

The items inherit QGraphicsItem and QObject, and have Q_PROPERTY macros for the pos() and rotation() of the QGraphicsItem to enable animating them with the Qt Animation framework.
I perform transformations on the items, such as scaling and rotating (the latter only in the in-development multitouch branch), and I also use QGraphicsDropShadowEffect on them.

我使用 QGLWidget 作为 QGraphicsView 的视口,以便为应用启用 OpenGL 加速.

I use a QGLWidget as viewport of the QGraphicsView in order to enable OpenGL acceleration for the app.

问题是,尽管使用了 OpenGL 加速,但该应用程序根本不流畅.(特别是动画,特别是因为我在多点触控分支中添加了旋转变换.)显示的图形项目不多,也没有 3D 操作或任何严重的事情,只是 2D 绘图.
我根本不是图形专家,所以我不知道为什么这个应用程序运行缓慢.我见过效果更复杂的其他游戏运行起来比这更流畅.

The problem is that despite being OpenGL-accelerated, the app is not smooth at all. (Especially the animations and especially since I added the rotation transform to the multitouch branch.) There are not many graphics items displayed, and there are no 3D operations or anything serious, just 2D drawing.
I'm not a graphics expert at all, so I have no idea why this app runs slowly. I've seen other games with lot more complicated effects run a lot smoother than this.

秘诀是什么?我该如何优化此应用?

What's the secret? How could I optimize this app?

推荐答案

好的,我等了这么久才找到解决方案.

Okay, I've waited for this long for a solution.

同时,我用 QML 重写了应用的 UI,令我惊讶的是,性能提升了很多,应用现在非常流畅.

In the meantime, I've rewritten the app's UI in QML, and to my surprise, the performance is a LOT better and the app is very smooth now.

一些结论:

  • 在全屏模式下运行时,OpenGL 加速效果最佳.将整个 UI 放在 QDeclarativeView 中并将其 viewPort 设置为 QGLWidget,并以全屏方式显示使这成为可能.
  • 似乎 QWidgets 的开销比我们想象的要多得多.
  • QML 的表现比预期的要好得多.
  • QGraphicsDropshadowEffect 的影响很小,但我删除了它,现在我改用笔触效果.将来,我可能会考虑使用 QML 着色器效果.
  • 值得为 QDeclarativeView 设置各种优化标志
  • 使用 alpha 透明度绘制项目比不使用它们绘制效果差很多.尽可能避免使用 Alpha 透明度.
  • 将 QGraphicsItem 子类重写为 QDeclarativeItem 子类非常简单且值得付出努力.

源代码在这里.

这篇关于如何优化基于 QGraphicsView 的应用程序的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆