QT5:使用drawPixMap()在另一个矩形的顶部绘制矩形

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

本文关键字:顶部 绘制 另一个 使用 drawPixMap QT5      更新时间:2023-10-16

我是否正确使用drawPixmap()

从本质上讲,我的目标是获取一个平铺集图像,并用自定义平铺图像替换单个平铺。

我可以在屏幕上加载这两个图像,但当我调用drawPixmap()时,原始图像根本不会改变。

提前谢谢。

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

编辑:

这就是replaceCustomTile的名称:

QPixmap terrainTiles(":/static/Terrain.png");
QPixmap customTile(":/static/Smiles.png");
replaceCustomTile(customTile, terrainTiles, 0);

要通过this初始化QPainter,如果您想在某个小部件上绘制它,则必须从小部件paintEvent(QPaintEvent *)调用它。因此,在这种情况下,应该从事件处理程序调用replaceCustomTile()

要在另一个像素图的顶部绘制某个像素图,QPainter应使用QPainter::begin():由目标像素图初始化

QPainter painter;
painter.begin(&target);
painter.drawPixmap(rect, custom);
painter.end();

上述代码通过CCD_ 11将CCD_ 9绘制为给定的CCD_。对target进行了修改。