Search code examples
c++qtqlabelqpixmap

Problems with merging multiple QPixmaps


I'm developing a painter-like application and i want my application to have multiple layers for different drawings. For that purpose i have an array, that contains QPixmaps with transparent background and i have a function that merges two QPixmaps (draws one on another). It's done like so:

    void MeasuresWidget::MergePixmaps(QPixmap source, QPixmap target)//draw source on target
{
    QPainter painter(&target);
    painter.drawPixmap(target.rect(),source,source.rect());
    painter.end();
    imageLabel->setPixmap(target);
}

I'm for 100% sure that array of pixmaps (AllLayers array) contains all the drawing i want. Now i want to consistently merge all the drawings with the original image. Here's how i'm trying to achieve this:

void MeasuresWidget::on_actionAct_triggered()
{
    ForMerging = &OriginalImage;
    for(int i=0;i<5;i++)
        MergePixmaps(AllLayers[i], *ForMerging);
}

where ForMerging is temporary QPixmap object for, well, merging, and OriginalImage is, undoubtedly, QPixMap, that contains original image. Again, i'm 100% sure, that all layers contain it's image on transparent background. The problem i'm facing is that in result original image is merged only with the last drawing, i.e. with AllLayers[4]. If i make i to run from 0 to 2(not including), for example, the result will be original image merged only with AllLayers[1]. I've struggled with this problem for a period of time and have no idea what might be wrong so i'm seeking for any help possible.


Solution

  • Try merge all QPixmap at the same call to MergePixmaps. For this change your Source variable at MergePixmap function to your AllLayers object like this:

    void MeasuresWidget::MergePixmaps(AllLayers *source, QPixmap target)//draw source on target
    {
        QPainter painter(&target);
        for(int i = 0; i < source->lenght();i++){ 
           painter.drawPixmap(target.rect(),source->at(i),source->at(i).rect());
        }
        painter.end();
        imageLabel->setPixmap(target);
    }