PDA

View Full Version : Maximum Size of QPixmap/QImage Windows



pssss
11th February 2011, 10:55
I have a QGraphicsView for a very wide QGraphicsScene. I need to draw the background in drawBackground() and the background is a bit complicated (long loop) although it doesn't need to be repainted constantly. I store it in a static QPixmap (I tried QImage too) inside the function drawBackground() and that pixmap is what I draw onto the painter of the view. Only when needed is the QPixmap painted on again.

If I didn't use a static pixmap, the complicated background would be generated every time I scroll sideways for example. The problem is that apparently there is a maximum width for pixmaps on Windows, on my computer it's 32770. I could store a list of pixmaps and draw them side by side but it would make the code uglier and I also don't know what the maximum width of a pixmap is for every Windows machine. Since this might be a well-known problem I was wondering if anyone has a better solution.


Thanks.

high_flyer
11th February 2011, 11:17
If I didn't use a static pixmap, the complicated background would be generated every time I scroll sideways for example. The problem is that apparently there is a maximum width for pixmaps on Windows, on my computer it's 32770. I could store a list of pixmaps and draw them side by side but it would make the code uglier and I also don't know what the maximum width of a pixmap is for every Windows machine. Since this might be a well-known problem I was wondering if anyone has a better solution.
Have a look at QPainter::drawTiledPixmap().

pssss
11th February 2011, 11:30
Thanks, I didn't know about that. Although that only solves it for the part of the pixmap that stays the same, other part of the pixmap would change from tile to tile.

high_flyer
11th February 2011, 11:51
Then there are two options I see:
1. Don't use drawBackround, rather, put your pixmaps in items.
That way, you don't waste memory, and let QGraphicsScene control which pixmaps are drawn (only the visible ones) -which is very efficient.
2. Implement the visibility check on the pixmaps you self, and only draw the visible ones in drawBackground.

pssss
11th February 2011, 12:58
Thanks again for answering. 1 would use more memory, what I can't use with tiled pixmaps because it changes is text and I'd prefer to avoid the overhead of having to use a qgraphicsitem for each piece of text of which there are many. I like 2 but I'm looking at the API and I don't see any function to get the visible area of the scene from the view.

There is a scrollbar and sceneRect() would return the whole scene (the whole scene is visualized) but not the smaller Rect that is visible on screen when the function is called. I could get the value of the scrollbar and work from there but it would be cumbersome, isn't there any function that gets me that x coordinate I want? QWidget's visibleRegion doesn't work either.



Ok I found the solution to my last question:

QRect portRect = viewport()->rect();
QRectF sceneRect = mapToScene(portRect).boundingRect();

sceneRect.x() will have the coordinate I want.


Thanks again

JohannesMunk
11th February 2011, 13:07
Hi!

Use the exposedRect that a graphicsitem::paint function recieves. You will need to create an item for your background set its z order to a low number and implement your own caching method..



CustomItem::CustomItem()
{
// Need this for exposedRect to be initialized!
setFlag(QGraphicsItem::ItemUsesExtendedStyleOption ,true);
}

void CustomItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
// Use the exposedRect
option->exposedRect ..
..
}

HIH

Johannes