[英]QT5: Drawing rect on top of another rect using drawPixMap()
我是否正確使用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();
上面的代碼將QPixmap custom
繪制到QPixmap target
給定的QRect rect
。 target
已修改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.