如何在QT中覆盖2个图像

发布于 2025-02-04 06:09:52 字数 562 浏览 4 评论 0原文

我有2个我在qimage中加载的图像,我的问题是: 如何以最简单的方式叠加这两个图像,然后将其保存在qpixmap中?

这是以下图像: “ “

和已久的结果:

(最终图像将在qtableView中使用,以显示用户是否具有相同的药水)

I have 2 images that I load in QImage and my question is :
How can I overlay these 2 images in the simplest way, to then save it in a QPixmap ?

Here are the following images : potionstack multiplier

And the awaited result : result

(The final image will be used in a QTableView to show the user if it has more of the same potion)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯了 2025-02-11 06:09:52

经过更多的研究,我能够自己找到答案。
这是我想到的代码(如果您能做得更好,我想看它,因为如果有的话,我想实现任何更好的方法):

    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

I was able to find the answer on my own after some more research.
Here is my code that I came up with (if you can do it better I would like to see it because I would like to implement any better approach if there is any):

    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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文