简体   繁体   中英

QT5: Drawing rect on top of another rect using drawPixMap()

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM