[solved] How to work with masks in QPainter?



  • Hello,
    I'm using Qt.5.3 and want to work with masks on a QImage to fill a region with a specific color.

    My mask is a 1Bit QImage (Mono).

    i thought, that i can handle this like:

    @
    QPainter overlayPainter(&overlayClusters);
    QPixmap pixmap = QPixmap::fromImage(maskForegroundByUser);
    overlayPainter.setClipRegion(QRegion(pixmap));
    overlayPainter.fillRect(overlayClusters.rect(),brush);
    @

    but i can't create a QRegion from a QPixmap.

    Can someone help me?

    //EDIT
    This solution works and has good performance:
    @
    QPainter overlayPainter(&image);
    QBitmap bitmap = QBitmap::fromImage(mask); //mask is a QImage with the format Mono
    QRegion regionMask(bitmap);
    overlayPainter.setClipRegion(regionMask);
    overlayPainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
    QBrush brush(Qt::blue,Qt::SolidPattern);
    overlayPainter.fillRect(image.rect(),brush);
    @
    Greetings


  • Lifetime Qt Champion

    Hi,

    I haven't played with mask yet, but from the doc you can make a QBitmap from your maskForegroundByUser to use with setClipRegion

    Hope it helps



  • Thanks,
    I thought about it, too. Now I see, that the QBitmap have a finction called "fromImage()".

    That works!


  • Lifetime Qt Champion

    Good !

    Since your mask is working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.