QWidget.setMask() problem

  • 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:
      painter.setClipping( true );
      painter.setClipRect( 0, 0, width(), height() );
      painter.setClipRegion(QRegion(QRect(rect())).subtracted(QRegion(QBitmap( imagePixmap))));
      painter.drawPixmap( rect(), imagePixmap );
    2. btn.setMask:
      setMask( QBitmap( imagePixmap ) );

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

    What to do?)

  • Moderators

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

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

  • 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...

Log in to reply