[PyQt] Bug with QWidget masks in QGraphicsScene?

jgillula+pyqt at gmail.com jgillula+pyqt at gmail.com
Tue Mar 16 05:17:18 GMT 2010


Hello,

I have a question (which may stem from either a bug in PyQt, Qt, or just 
my understanding of how Qt is supposed to work).  In particular, 
according to the Qt documentation 
(http://doc.trolltech.com/4.6/qwidget.html#setMask) masked widgets are 
only supposed to receive mouse events on their visible portions.  This 
works fine on a normal widget in a GUI, but doesn't seem to be the case 
if I add a widget to a QGraphicsScene via the addWidget method.

In particular, some example code is below (at the end of my message).  
In this example I create two buttons, and place one in a QGraphicsScene 
and the other as part of a normal layout.  I then add a simple mask so 
that only the left half of each button should be visible.  The painting 
of both buttons seems to be handled correctly (i.e. only the left half 
gets painted), but the mouse events do not.  In particular, if I click 
the non-visible part (right half) of the normal-layout-button, nothing 
happens.  But if I click the invisible right half of the QGraphicsScene 
button, the button still appears to be receiving the mouse click event.

So, my question is...is this a bug in PyQt?  Or does the same behavior 
happen under Qt in other languages?  Or is it not a bug at all, and 
QWidgets are not supposed to respect their masks with regard to mouse 
events when added to QGraphicsScenes?  Or am I just totally crazy?  ;-)

Any advice or suggestions anyone can provide would be very much appreciated!

 - Jeremy

Example Code:
from PyQt4.QtCore import Qt, QRect
from PyQt4.QtGui import QApplication, QMainWindow, QGraphicsScene, 
QPushButton, QGraphicsView, QRegion, QWidget, QVBoxLayout

def gotAClick():
  print "Got a click";

if __name__ == '__main__':
  import sys

  app = QApplication(sys.argv)

  button1 = QPushButton("1111111");
  button2 = QPushButton("2222222");
  button1.clicked.connect(gotAClick)
  button2.clicked.connect(gotAClick)
  graphics_scene = QGraphicsScene();
  graphics_view = QGraphicsView(graphics_scene);

  graphics_scene.addWidget(button1)
 
  window = QMainWindow();
  main_widget = QWidget();
  main_layout = QVBoxLayout();
  main_layout.addWidget(graphics_view);
  main_layout.addWidget(button2);
  main_widget.setLayout(main_layout);
  window.setCentralWidget(main_widget);
  window.show();

  button1.setMask(QRegion(QRect(0,0,button1.width()/2, button1.height())));
  button2.setMask(QRegion(QRect(0,0,button2.width()/2, button2.height())));

  sys.exit(app.exec_())



More information about the PyQt mailing list