簡體   English   中英

QT5:使用drawPixMap()在另一個矩形的頂部繪制矩形

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM