我在 QImage 中加載了 2 張影像,我的問題是:如何以最簡單的方式疊加這 2 張影像,然后將其保存在 QPixmap 中?
以下是以下圖片:

期待的結果:
(最終的影像將在 QTableView 中使用,以顯示用戶是否有更多相同的藥水)
uj5u.com熱心網友回復:
經過更多研究,我能夠自己找到答案。這是我想出的代碼(如果你能做得更好,我希望看到它,因為如果有的話,我想實作任何更好的方法):
QPixmap base = QPixmap::fromImage(QImage("Your 1st img"));
QPixmap overlay = QPixmap::fromImage(QImage("your 2nd img"));
QPixmap result(base.width(), base.height());
result.fill(Qt::transparent); // force alpha channel
{
QPainter painter(&result);
painter.drawPixmap(0, 0, base);
painter.drawPixmap(0, 0, overlay);
}
QStandardItem *pCombinedItem = new QStandardItem(); //this variable should be in the .h file if you want to conserve it further on.
pCombinedItem->setData(QVariant(result), Qt::DecorationRole);//adding the final img into the StandardItem which we can put then into our table after we put it into a StandardItemModel like so :
model->setItem(1,4,pCombinedItem);
pInventory->setModel(model); //and we can put our model into our tableview
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/487380.html
