This is caused by the fact that the event handling is on the QLabel and even though you think you are clicking on blank space, its just the part of the QLabel where the image no longer covers. You have the QLabel inside of a layout, which means it will expand when you resize the parent widget. So you have a couple options here.
1) You can set label.setScaledContents(True) and have the icon resize to always fit the QLabel, 2) You can set the QLabel to not resize. This is done by calling label.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed) It also means you will have gap between the button and image, so you could also call layout.addStretch() after adding them, to keep them pinned to the top. Justin On Fri, Dec 7, 2018 at 8:59 AM likage <[email protected]> wrote: > Hi all, > > I am trying to create a 'clicked' signal where it will only be triggered > if User clicks anywhere within the icon. > In the following code: > > class TestWin(QtGui.QWidget): > def __init__(self, parent=None): > super(TestWin, self).__init__(parent) > self.init_ui() > > def init_ui(self): > > lyt = QtGui.QVBoxLayout() > btn = QtGui.QPushButton('test button') > > lbl = QtGui.QLabel() > icon_path = '/Desktop/people.png' > lbl.setPixmap(icon_path) > > lyt.addWidget(btn) > lyt.addWidget(lbl) > > self.setLayout(lyt) > > # Signals > btn.clicked.connect(self.btn_click) > lbl.mousePressEvent = self.lbl_click > > def btn_click(self): > print 'I am clicking on the button...' > > def lbl_click(self, event): > print 'I am clicking on the label...' > > > my_win = TestWin() > my_win.show() > > As soon as I resize the window bigger, clicking on any blank space will > still prints the message 'I am clicking on the label' > > I also tried using event filtering where as follows: > > class TestWin(QtGui.QWidget): > def __init__(self, parent=None): > super(TestWin, self).__init__(parent) > self.init_ui() > > def init_ui(self): > ... > ... > > # Signals > btn.clicked.connect(self.btn_click) > lbl.installEventFilter(self) > > def btn_click(self): > ... > > def eventFilter(self, source, event): > if event.type() == QtCore.QEvent.MouseButtonPress: > print "The sender is:", source.text() # Returns me blank > return super(TestWin, self).eventFilter(source, event) > > but the `source.text()` is returning me nothing. > > Any insights are appreciated! > > > -- > You received this message because you are subscribed to the Google Groups > "Python Programming for Autodesk Maya" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/python_inside_maya/4913a8b5-eb22-4ffd-a3d2-268dc029ab8c%40googlegroups.com > <https://groups.google.com/d/msgid/python_inside_maya/4913a8b5-eb22-4ffd-a3d2-268dc029ab8c%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1uCThtuwDbVhBoF4q0_N6Da%2B1dXoteoZTaBj8tj3gP7A%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
