Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] How to work with masks in QPainter?
Qt 6.11 is out! See what's new in the release blog

[solved] How to work with masks in QPainter?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sheepy
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sheepy
        wrote on last edited by
        #3

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

        That works!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved