【问题标题】:overlapping of image in QTQT中图像的重叠
【发布时间】:2015-07-25 08:11:49
【问题描述】:

我有两张图片。我需要将图像相互叠加/重叠。第一个图像或基础图像的大小为 160x128,让第二个图像为 120x100。如何相互重叠?

这是我的代码,我把它当作参考:

https://idvlpsw.wordpress.com/2008/03/19/use-qimage-to-create-a-composite-image-ie-one-image-with-another-overlaid-on-top-of-it/

#include "mainwindow.h"
#include <QApplication>
#include "mainwindow.h"
#include <QPixmap>
#include <QFile>
#include <QLabel>
#include <QPainter>


QLabel *lbl= NULL ;

QImage baseImage("/usr/image1.jpg");
QImage overlayLogoff("/usr/image2.jpg");


QImage createImageWithOverlay(const QImage& baseImage, const QImage& overlayImage)
{
    QImage imageWithOverlay = QImage(baseImage.size(), QImage::Format_RGB16);
    QPainter painter(&imageWithOverlay);

    painter.setCompositionMode(QPainter::CompositionMode_Source);
    painter.fillRect(imageWithOverlay.rect(), Qt::transparent);

    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
    painter.drawImage(0, 0, baseImage);

    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
    painter.drawImage(0, 0, overlayImage);

    painter.end();

    return imageWithOverlay;
}

QImage logoffImage = createImageWithOverlay(baseImage, overlayLogoff);

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    lbl = new QLabel();

    createImageWithOverlay(baseImage, overlayLogoff);
return a.exec();
}

在这里我无法获得任何图像?我是否需要使用显示功能来显示图像。

请帮忙。谢谢

【问题讨论】:

    标签: qpainter qimage qt4.8


    【解决方案1】:

    查看您的代码,我想您正试图在 QLabel 中显示合成图像。
    但要使这段代码工作,合成图像必须附加到 QLabel,并且 QLabel 本身必须可见:

    int main(int argc, char *argv[])
    {
       QApplication a(argc, argv);
    
       QLabel * lbl = new QLabel();
       QImage composite = createImageWithOverlay(baseImage, overlayLogoff);
       lbl->setPixmap(QPixmap::fromImage(composite));
       lbl->show();
    
       return a.exec();
    }
    

    PS:你应该尽量避免在全局范围内定义变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      相关资源
      最近更新 更多