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. QWidget.setMask() problem
Qt 6.11 is out! See what's new in the release blog

QWidget.setMask() problem

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.5k 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.
  • R Offline
    R Offline
    rom8726
    wrote on last edited by
    #1

    Hi! I can't create a button, which must have image's shape. Image (PNG) have transparent areas, and I want to clip mouse events on transparent areas...
    I have tried 2 ways, but mouse events whatever works in transparent area:

    1. reimplement paintEvent:
      [code]
      painter.setClipping( true );
      painter.setClipRect( 0, 0, width(), height() );
      painter.setClipRegion(QRegion(QRect(rect())).subtracted(QRegion(QBitmap( imagePixmap))));
      painter.drawPixmap( rect(), imagePixmap );
      [/code]
    2. btn.setMask:
      [code]
      setMask( QBitmap( imagePixmap ) );
      [/code]

    and just in case I have reimplemented mouseMoveEvent:
    [code]
    if( !mask().contains( event->pos() ) )
    {
    return; //exit from handling
    }
    [/code]

    What to do?)

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      ad 1):
      doing anything related to mouse events wont work since the paintEvent()'s purpose is just drawing

      as 2)
      i think the mask only works for top-level widget...but i'm not sure right now.
      But you may also want to check if the QBitmap is correct by saving it to a file and check it if black and white colors are correct and what are you expecting.
      @
      QBitmap bitmap( imagePixmap );
      bitmap.save("<any-file-path>");
      @

      ad 3)
      i don't know which mouse events you mean, but i think you mean mouse clicks? If so you should rather do this in mousePressEvent() and mouseReleaseEvent()

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rom8726
        wrote on last edited by
        #3

        Thanks.
        3) My button changing background image when user moving mouse on button, and I not want image to be changed when mouse at transparent area...

        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