Search code examples
c++qtqpixmap

Add a border to a QPixmap


I load a QPixmap from a file. I want to add a border around the QPixmap, say 4px white on each side.

What is the fastest way to do this? Do I have to create a second larger QPixmap and draw the first into the second or is there some way to extend the existing one without scaling (I've noticed that there is a QPixmap::transformed() but don't know if this is suitable in my case.


Solution

  • Do I have to create a second larger QPixmap and draw the first into the second

    Yes.

    is there some way to extend the existing one without scaling

    Yes. The larger pixmap will take an unscaled copy of the original one.

    The foregoing assumes that you care about the resulting pixmap. If you don't care about it, but only care to display the pixmap on a window/widget with some border, then simply draw the border and pixmap separately using QPainter, OpenGL geometry, etc.