Reputation: 127
am I using drawPixmap()
correctly?
Essentially my goal is to take an tileset image, and replace an individual tile with a custom tile image.
I'm able to get both images to load on the screen, but when I call drawPixmap()
, then original image doesn't change at all.
Thanks in advance.
void replaceCustomTile(QPixmap custom, QPixmap target, int whichTile) {
QRect rect(0, 0 + (squareTileSize * whichTile), squareTileSize, squareTileSize);
QRect customRect = custom.rect();
QPainter painter(this);
painter.drawPixmap(rect, target, customRect);
painter.end();
}
EDIT:
This is how replaceCustomTile is called:
QPixmap terrainTiles(":/static/Terrain.png");
QPixmap customTile(":/static/Smiles.png");
replaceCustomTile(customTile, terrainTiles, 0);
Upvotes: 1
Views: 1167
Reputation: 6776
To intialize QPainter
by this
it must be called from the widget paintEvent(QPaintEvent *)
if you want to draw it on some widget. So, replaceCustomTile()
should be called from the event handler in that case.
To draw some pixmap on top of another pixmap QPainter
should be initialized by the target pixmap using QPainter::begin()
:
QPainter painter;
painter.begin(&target);
painter.drawPixmap(rect, custom);
painter.end();
The above code draws QPixmap custom
into given QRect rect
over QPixmap target
. The target
is modified.
Upvotes: 2