Modifying

There are two ways to modify image pixel data. The first one works only for QImage and involves direct manipulation of pixels using the setPixel() call, which takes the pixel coordinates and color to be set for that pixel. The second one works for both QImage and QPixmap and makes use of the fact that both these classes are subclasses of QPaintDevice. Therefore, you can open QPainter on such objects and use its drawing API. Here's an example of obtaining a pixmap with a blue rectangle and red circle painted over it:

QPixmap px(256, 256); px.fill(Qt::transparent); QPainter painter(&px); painter.setPen(Qt::NoPen); painter.setBrush(Qt::blue); QRect r = px.rect().adjusted(10, 10, -10, -10); painter.drawRect(r); painter.setBrush(Qt::red); ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.