QT-有没有将几个图像组合成一个的类

QT - Is there any class for combine few images into one?

本文关键字:一个 组合 几个 有没有 QT- 图像      更新时间:2023-10-16

我想做一些图像矩阵,在一些小部件中显示预览,然后将其保存到(例如)jpg文件中。我知道我可以把每个图像像素复制成一个大像素,但我想这不是一个有效的方法。。。有更好的解决方案吗?

谢谢你的建议。

我不复制单个像素,而是直接在足够大的QPixmap上绘制每个单独的图像,以容纳所有图像。然后可以通过在拼贴画上绘制每个单独的图像来生成拼贴画,如下所示(未经测试的代码):

QList<QPixmap> images;
QPixmap collage;
// Make sure to resize collage to be able to fit all images.
...
for (QList<QPixmap>::const_iterator it = images.begin(); it != images.end(); ++it)
{
    int x = 0;
    int y = 0;
    // Calculate x & y coordinates for the current image in the collage.
    ...
    QPainter painter(&collage);
    painter.drawPixmap(
            QRectF(x, y, (*it).width(), (*it).height()), *it,
            QRectF(0, 0, (*it).width(), (*it).height()));
}

注意,也可以使用QImage来代替QPixmapQPixmap针对屏幕显示进行了优化。有关更多详细信息,请参阅Qt文档。

不,您不想逐个像素地执行此操作。QImage是一个QPaintDevice。因此,您可以加载它们,将它们相互渲染,并根据需要以多种格式保存。当然在屏幕上显示它们。

上面的代码对我不起作用,我不知道为什么。

我需要的是一幅拼贴画:
图片A|PicB|图片…|…|
我在QImages中发现了类似的东西,这段代码也能正常工作。
(测试代码):

const int S_iconSize = 80;     //The pictures are all quadratic.
QList<const QPixmap*> t_images;//list with all the Pictures, PicA, PicB, Pic...
QImage resultImage(S_iconSize*t_images.size(), S_iconSize, QImage::Format_ARGB32_Premultiplied);
QPainter painter;
painter.begin(&resultImage);
for(int i=0; i < t_images.size(); ++i)
{
    painter.drawImage(S_iconSize*i, 0, t_images.at(i)->toImage(), 0, 0, S_iconSize, S_iconSize, Qt::AutoColor);
}
painter.end();
QPixmap resultingCollagePixmap = QPixmap::fromImage(resultImage);

我知道这很难看,因为QImages被转换为QPixmap,反之亦然,但它是有效的。因此,如果有人知道如何运行上面的代码(来自Ton van den Heuvel),我会很高兴。(也许只是缺少一个QPainter??)

问候

我做了以下操作:

// Load the images in order to have the sizes at hand.
QPixmap firstPixmap(":/images/first.png");
QPixmap secondPixmap(":/images/second.png");
const int gap = 25;
// Create an image with alpha channel large enough to contain them both and the gap between them.
QImage image(firstPixmap.width() + gap + secondPixmap.width(), firstPixmap.height(), QImage::Format_ARGB32_Premultiplied);
// I happen to need it transparent.
image.fill(QColor(Qt::transparent));
// Paint everything in a pixmap.
QPixmap pixmap = QPixmap::fromImage(image);
QPainter paint(&pixmap);
paint.drawPixmap(0, 0, firstPixmap);
paint.drawPixmap(firstPixmap.width() + gap, 0, secondPixmap);
// Use it.
QIcon icon(pixmap);
[...]