当前位置: 代码迷 >> QT开发 >> 大家帮忙看看,怎么加快这个Qt程序的运行速度
  详细解决方案

大家帮忙看看,怎么加快这个Qt程序的运行速度

热度:67   发布时间:2016-04-25 04:30:40.0
大家帮忙看看,如何加快这个Qt程序的运行速度?
开辟动态空间 QGraphicsItem *pnode = new QGraphicsItem[6376] (QGraphicsItem是QT里面的一个类)

scene = new QGraphicsScene(0, 0, Xmax*VisZoom, Ymax*VisZoom);

然后通过scene->addItem(&pnode)把6376个QGraphicsItem 的对象加到scene上显示,如下图:(每个小红点就是一个GraphicsItem 对象)



但是这样一来,当我同时变化6376个QGraphicsItem的大小时,程序非常慢,要半分钟才能变化完成。

问问大家如何提高运行速度啊?

谢谢


------解决方案--------------------
void QGraphicsItem::setCacheMode ( CacheMode mode, const QSize & logicalCacheSize = QSize() )

Sets the item's cache mode to mode.

The optional logicalCacheSize argument is used only by ItemCoordinateCache mode, and describes the resolution of the cache buffer; if logicalCacheSize is (100, 100), QGraphicsItem will fit the item into 100x100 pixels in graphics memory, regardless of the logical size of the item itself. By default QGraphicsItem uses the size of boundingRect(). For all other cache modes than ItemCoordinateCache, logicalCacheSize is ignored.

Caching can speed up rendering if your item spends a significant time redrawing itself. In some cases the cache can also slow down rendering, in particular when the item spends less time redrawing than QGraphicsItem spends redrawing from the cache. When enabled, the item's paint() function will be called only once for each call to update(); for any subsequent repaint requests, the Graphics View framework will redraw from the cache. This approach works particularly well with QGLWidget, which stores all the cache as OpenGL textures.

Be aware that QPixmapCache's cache limit may need to be changed to obtain optimal performance.

You can read more about the different cache modes in the CacheMode documentation.

This function was introduced in Qt 4.4.

See also cacheMode(), CacheMode, and QPixmapCache::setCacheLimit().

这是从Qt的帮助中复制过来的,你的问题我没处理过,只是看到这些应该会对你有帮助。
------解决方案--------------------
处理缓冲。
------解决方案--------------------
受教了!!!!!!!!!1
------解决方案--------------------
用QGraphicsItem的advance函数试试?
QGraphicsScene的advance slot会调用其上所有item的advance函数
------解决方案--------------------
#2 楼CtrlV的那个信息不错啊,楼主怎么调用哪函数的?
------解决方案--------------------
和选择的painter engine(opengl,... )有没有关系啊,胡乱猜的.
试试到http://www.qtcentre.org/ 去搜搜相关帖子
------解决方案--------------------
把数组做到一个item里面试试
------解决方案--------------------
请使用QGrahicsView作为QGrahicsScene的显示部件
QGraphicsView有个setCatchMode(CatchMode mode)方法,参数是个枚举:
GraphicsView::CacheNone
QGraphicsView::CacheBackground
文档对QGraphicsView::CacheBackground的解释为:
QGraphicsView将分配一张与视口大小一致的QPixmap作为绘图缓冲区
意思就是先在QPixmap上画Items,OK以后再把这张QPixmap画到界面显示
------解决方案--------------------
楼主,强烈建议你调试一下试试。
使用Qtime在程序中加入计时器,就可以看到哪段程序运行用了多少ms,这样再有针对性的改。

http://blog.csdn.net/ypoflyer
------解决方案--------------------
我做过一个GPS显示地图的程序, 也遇到过类似的问题。当时我用QTime调试发现是删除item用时比较长,我看你程序中用了clearSelecttion, 你不妨测试一下这个地方用了多长时间。 给你个大概的代码,你试一下吧:
void iItems::setupNode2(Node *node)
{
node->setScale(1/(float)spinBox2->value());
QTime addItemTimer;
addItemTimer.start();
scene->addItem(node);/////////////////////////////////////////
qDebug("Adding all items costs about : %d ms", addItemTimer.elapsed()*6376 );
  相关解决方案