O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required