QT - 是否有任何课程将少量图像合并为一个?

时间:2011-06-25 13:17:19

标签: c++ qt

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

感谢您的建议。

4 个答案:

答案 0 :(得分:6)

不是复制单个像素,而是直接在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 documentation

答案 1 :(得分:0)

不,你不想逐像素地做这个像素。 QImageQPaintDevice。因此,您可以加载它们,将它们相互渲染并按照您的喜好将它们保存为多种格式。当然,也可以在屏幕上显示它们。

答案 2 :(得分:0)

上面的代码对我不起作用,我无法弄清楚原因。

我需要的是拼贴画:
 PicA | PicB | PIC ... | ...... |
我找到了与QImage类似的东西,这段代码也有效。
(测试代码):

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);

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

此致

答案 3 :(得分:0)

我做了以下事情:

// 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);
[...]
相关问题