Search code examples
qt4qpainterqpixmappaintevent

How to stop the background from repeating in a QpaintEvent


I have a paintEvent and I'm adding the background everytime I paint something, the problem is that I need the image to stop repeating.

For example if my background image is 20pixels x 20 pixels, and my paint area is 40pixels x 40pixels, the background is 4 images, and i need only one.

Here is my code.

1. QPainter painter; // painter for the canvas
2. painter.begin(this);
3. QPixmap backgroundImage;
4. backgroundImage.load("multimedia/monitor_image.png");
5. painter.fillRect(event->rect(), QBrush(backgroundImage));

Thanks for the help!


Solution

  • There are a bunch of functions available to help scale the picture:

    http://doc.qt.nokia.com/4.7-snapshot/qpixmap.html

    Try "scaleToHeight" if you know the hight.

    Something like this should work:

    backgroundImage = backgroundImage.scaledToHeight(painter.height);
    

    Do that before you call fillRect, if its a perfect square you shouldn't have issues.