[Qt-interest] [SOLVED] QPixmap and transparency map generation

Francisco Ares frares at gmail.com
Tue Oct 6 02:06:22 CEST 2009


I know it was a very specific question, so I decided to spend some (a lot
of) hours to find this solution.  Probably there is another and more elegant
way of doing this, someday I'll figure it out ;-)

I have theese:
    QPolygonF * mPolygonF;   // delimiting points to select part of a bigger
image
    QImage * mOriginalImage;    // original image, from which I want to
select a part of

and I want a QPixmap with the size of the polygon's bounding rectangular
area, showing the polygonal shaped area of the selection by means of a
transparent mask.

    // this extracts the polygon's boundingRect from
    // the original image to get the partial image to work on
    QImage * mImage =
mOriginalImage->copy(mPolygonFout->boundingRect().toRect());

    // final pixmap declaration and size definition
    QPixmap PMap(mPolygonF->boundingRect().toRect().size());

    // already adds a transparency mask;
    // this took me a long time to figure out,
    // since there are other ways of tranforming
    // an image to a pixmap.
    PMap.fill(Qt::transparent);

    // now finally painting the partial image
    // to the pixmap
    QPainter painterPMap(&PMap);
    painterPMap.drawImage(QPoint(0, 0), *mImage);

    // declaration and initial filling of the
    // bitmap that will be the transparency mask.
    QBitmap BMap(PMap.mask());
    BMap.fill(Qt::color0);

    // declaration and setup of the painter
    // that will draw the shape of the polygon
    // on the transparency mask.
    QPainter painterBMap(&BMap);
    painterBMap.setPen(Qt::NoPen);
    painterBMap.setBrush(Qt::color1);

    // this also took some time: the polygon
    // must be shifted, as the new image is
    // just a part of the original
    int XMin = mPolygonF->boundingRect().toRect().x();
    int YMin = mPolygonF->boundingRect().toRect().y();

    // a QPolygon instead of a QPolygonF
    // is needed, so one is created and
    // shifted.
    QPolygon Poli(mPolygonF->toPolygon());
    Poli.translate(-XMin, -YMin);

    // a PainterPath converted from the polygon.
    QPainterPath painterPathBMap;
    painterPathBMap.addPolygon(Poli);
    painterPathBMap.closeSubpath();

    // final polygon (converted) drawing.
    painterBMap.drawPath(painterPathBMap);

    // and finally set to the pixmap.
    PMap.setMask(BMap);

Now the pixmap may be assigned to a QLabel or something like that to be
shown on scree.

I hope this helps someone else.

Francisco
-- 
"If you have an apple and I have an apple and we exchange apples then you
and I will still each have one apple. But if you have an idea and I have one
idea and we exchange these ideas, then each of us will have two ideas." -
George Bernard Shaw
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091005/bd883f7a/attachment.html 


More information about the Qt-interest-old mailing list