Discussion:
[Maya-Python] clicked signals around a QLabel
likage
2018-12-06 19:59:27 UTC
Permalink
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 python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/4913a8b5-eb22-4ffd-a3d2-268dc029ab8c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Justin Israel
2018-12-06 21:00:41 UTC
Permalink
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
Post by likage
Hi all,
I am trying to create a 'clicked' signal where it will only be triggered
if User clicks anywhere within the icon.
super(TestWin, self).__init__(parent)
self.init_ui()
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
print 'I am clicking on the button...'
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'
super(TestWin, self).__init__(parent)
self.init_ui()
...
...
# Signals
btn.clicked.connect(self.btn_click)
lbl.installEventFilter(self)
...
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
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 python_inside_maya+***@googlegroups.com.
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.
likage
2018-12-06 22:32:15 UTC
Permalink
Hi Justin,

Thank you for getting back. I had thought using the above example may help
me out but turns out not. :(
And so, I have decided to paste the code here in hopes of some guidance/
insights if any...

BaseWidget <https://pastebin.com/raw/KD0CQ9Ud>
GraphicsWidget <https://pastebin.com/raw/7KG9vkfT>

[image: example_widget.jpg]
The above 2 codes are used to created a widget (see attached) where I am
trying to induce a 'clicked' connection towards that play icon.

And it appears that I am unable to use either QPushButton or a QLabel to
replace it, by doing so, it will cause an error.
--
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 python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/985acba2-be81-422b-bd51-b84e149f530f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Justin Israel
2018-12-07 02:29:32 UTC
Permalink
Well you can't put a normal QWidget into a QGraphicsScene unless you wrap
it in a QGraphicsProxyWidget
<http://doc.qt.io/archives/qt-4.8/qgraphicsproxywidget.html>. But if you
start doing this a lot then you are going to start reducing your
performance back towards a normal QWidget setup. It would be better to just
implement your own clickable icon item with a QGraphicsPixmapItem
<http://doc.qt.io/archives/qt-4.8/qgraphicspixmapitem.html>.

Best to avoid QWidget as much as possible if you are in a QGraphicsScene.
Usually they are only used in proxies when you really need to integrate
existing widgets.

Justin
Post by likage
Hi Justin,
Thank you for getting back. I had thought using the above example may help
me out but turns out not. :(
And so, I have decided to paste the code here in hopes of some guidance/
insights if any...
BaseWidget <https://pastebin.com/raw/KD0CQ9Ud>
GraphicsWidget <https://pastebin.com/raw/7KG9vkfT>
[image: example_widget.jpg]
The above 2 codes are used to created a widget (see attached) where I am
trying to induce a 'clicked' connection towards that play icon.
And it appears that I am unable to use either QPushButton or a QLabel to
replace it, by doing so, it will cause an error.
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/985acba2-be81-422b-bd51-b84e149f530f%40googlegroups.com
<https://groups.google.com/d/msgid/python_inside_maya/985acba2-be81-422b-bd51-b84e149f530f%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 python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1_XdkAE9%2BU%2BD-dVWU%3DmZnFD%3DeGgotV_aW11JQBpwTKmg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...