• 9400阅读
  • 2回复

使用qt绘制背景图片的一种加速方法 [复制链接]

上一主题 下一主题
离线keisuo
 

只看楼主 正序阅读 楼主  发表于: 2007-05-10
— 本帖被 XChinux 执行加亮操作(2008-07-19) —
使用qt绘制背景图片的一种加速方法



在使用qt3.3.2开发实时系统的图形时,如果我们需要设置图形的背景图,需要使用下面的函数进行绘制:

       void QPainter::drawImage ( const QPoint &, const QImage &, const QRect & sr, int conversionFlags = 0 )或者是

       void QPainter::drawPixmap ( int x, int y, const QPixmap & pixmap, int sx = 0, int sy = 0, int sw = -1, int sh = -1 )

如果你在绘制大的背景图时使用了drawImage()函数,你将会发现背景图刷新的速度会比较慢,这一点在qt的文档中也进行了说明:

       There are functions to draw pixmaps/images, namely drawPixmap(), drawImage() and drawTiledPixmap(). drawPixmap() and drawImage() produce the same result, except that drawPixmap() is faster on-screen and drawImage() faster and sometimes better on QPrinter and QPicture.

还有在drawImage()函数的说明中:

       This function may convert image to a pixmap and then draw it, if device() is a QPixmap or a QWidget, or else draw it directly, if device() is a QPrinter or QPicture.

所以我们要做的第一步就是要把程序中使用drawImage()函数的地方转化为drawPixmap();

Ok,完成了上面的第一步之后,背景图刷新快多了,但出现了第二个问题又出现了,将QImage转换为QPixmap或者直接将图形加载为QPixmap依然很慢,这样就造成第一次打开某个图形文件时,背景图的加载会变得比较慢(当然,打开后只需要drawpixmap,就会比较流畅了),为了解决这个问题,我们设置如下结构:

       Struct file2qpixmap

{

       Char filename[256];

       QPixmap qpixmap;

};

将背景图片文件与相应的QPixmap数据结合起来做成一个全局的链表,每次打开图形文件后到链表中查找相应的背景图形对应的QPixmap数据是否已经存在,如果存在则直接使用,如果不存在,就插入一个新的节点.这样,对于使用同一副背景图片的图形文件而言,背景图形的QPixmap数据只需要加载一次.这样就再一次提高了图形文件刷新和切换的速度.

更进一步,我们可以把上述链表的内容写入内存映射文件或者数据库,每次程序启动的时候只需要将链表映射到当前程序的内存中就可以使用了,这样就进一步加快了背景图形的刷新,而且这样跳开了对qpixmap数据的加载.但这种方法我没有进行测试,有兴趣的朋友可以试一下.
[ 此贴被XChinux在2008-07-19 20:09重新编辑 ]
离线htyoung

只看该作者 2楼 发表于: 2007-05-15
注意: Qt4.2  已经有了很大的变化,如果是2d图形的处理 可以使用
class  QGraphicsView 

http://doc.trolltech.com/4.2/qgraphicsview.html

也可参考 安装目录下的
examples/graphicsview


copy from:  The Graphics View Framework

graphics View provides a surface for managing and interacting with a large number of custom-made 2D graphical items, and a view widget for visualizing the items, with support for zooming and rotation.
The framework includes an event propagation architecture that allows precise double-precision interaction capabilities for the items on the scene. Items can handle key events, mouse press, move, release and double click events, and they can also track mouse movement.
Graphics View uses a BSP (Binary Space Partitioning) tree to provide very fast item discovery, and as a result of this, it can visualize large scenes in real-time, even with millions of items.
Graphics View was introduced in Qt 4.2, replacing its predecessor, QCanvas. If you are porting from QCanvas, see Porting to Graphics View.
***
QT5
***
离线haiyong_wu

只看该作者 1楼 发表于: 2007-05-15
怎么没注明是转帖
快速回复
限100 字节
 
上一个 下一个