PDA

View Full Version : How to Optimize QGraphicsItem paint function in qtScript



snaeem
15th May 2012, 13:44
Hi Guys,

I want to develop Tiled Map Editor (http://www.mapeditor.org/) in QtScript. I have already tried following things:

Initially I was making a QGraphicsRectItem for each tile and each Tile Layer was also a QGraphicsRectItem. This worked well for small sized map. But as soon as I made a large map e.g. 300 x 300 cells each 32 x 32 pixels the performance degraded. It took quite long to make all the items and even after they were created scrolling was quite jerky.

I thought it was due to presence of too many objects in memory (around 90000 per tile-layer)

So I changed my implementation so that each tile-layer would be a single QGraphicsRectItem and I will logically handle the painting of each layer. Since the size of each such item was very big (32 * 300 x 32 * 300 pixels) paint event which was called on slightest scrolling became the bottle neck.

The QStyleOptionGraphicsItem received as second parameter of paint does not contain exposedRect and rect in QtScript unlike C++.
I have also tried to set ItemUsesExtendedStyleOptions flag but I do not get exposedRect or rect in QtScript. So I used graphicsView.mapToScene(0,0) and graphicsView.mapToScene(graphicsView.frameSize.wid th(),graphicsView.frameSize.height()); to calculate the area I need to re-paint myself. Though this improved the performance I faced another issue i.e. sometime while scrolling fast using mouse wheel grid was not painted correctly. If I scroll up and down slowly everything works fine.

I have used graphicsView.viewport().setAttribute(Qt.WA_StaticC ontents,true); to optimize performance.
I tried using cacheMode mode but since it did not increase the performance a great deal so I am not using it for now.

I have following questions:

1) I have attached a screen-shot and code I am using to draw the grid. Can you guys think of any possible reason why grid is not drawn correctly when I scroll up and down fast using mouse wheel.

2) Am I doing things the correct way. I mean having a very big item as a layer, is it correct was to do this. Are there any other flags that I can set. I have tried QGraphicsView.DontAdjustForAntialiasing and QGraphicsView.DontSavePainterState but they don't seem any positive effect.

Any help will be appreciated.