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

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

    Can someone help me?

    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);
    QBrush brush(Qt::blue,Qt::SolidPattern);

  • Lifetime Qt Champion


    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.