QPushButton hover

1,305 views
Skip to first unread message

osm...@gmail.com

unread,
Aug 19, 2018, 9:11:54 PM8/19/18
to Python Programming for Autodesk Maya
Hello. I have been trying to find a way to run a function when the mouse is on top of a button. I tried to overrride the QPushButton class, and the enterEvent function, but I dont have any results. This is what I have:

class CustomButtom(QtGui.QPushButton):

def __init__(self):
pass

def mouseMoveEvent(self, event):
print("in focus")

def enterEvent(self, event):
print("Enter")

def leaveEvent(self, event):
print("Leave")

I have an extra function to create the buttons, and in there I set the setMouseTracking(1) to 1.

What am I missing?. My guess is that I need to connect the enter and and leave event, but, to who?I thought they would be connected to the mouse movement already.

Thanks.

Justin Israel

unread,
Aug 19, 2018, 9:41:44 PM8/19/18
to python_in...@googlegroups.com
On Mon, Aug 20, 2018 at 1:11 PM <osm...@gmail.com> wrote:
Hello. I have been trying to find a way to run a function when the mouse is on top of a button. I tried to overrride the QPushButton class, and the enterEvent function, but I dont have any results. This is what I have:

class CustomButtom(QtGui.QPushButton):

    def __init__(self):
        pass

You broke the constructor by overriding it and not calling the parent constructor. Either remove this, or do:

    super(CustomButtom, self).__init__()


    def mouseMoveEvent(self, event):
        print("in focus")

    def enterEvent(self, event):
        print("Enter")

    def leaveEvent(self, event):
        print("Leave")

I have an extra function to create the buttons, and in there I set the setMouseTracking(1) to 1.

What am I missing?. My guess is that I need to connect the enter and and leave event, but, to who?I thought they would be connected to the mouse movement already.

Thanks.

--
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_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/693291aa-7811-42ad-8660-3e51b25ebe3c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

osm...@gmail.com

unread,
Aug 19, 2018, 10:04:37 PM8/19/18
to Python Programming for Autodesk Maya
Hi Justin. Thanks for taking some time to answer me. I tried removing it and also re-working the constructor, but I still have no results when my cursor hovers on top of the button:

https://pastebin.ubuntu.com/p/GtMq6qDZZ9/

thanks

Justin Israel

unread,
Aug 19, 2018, 10:25:26 PM8/19/18
to python_in...@googlegroups.com
Can you show me a complete example of how you are using the button in combination with your app? When I test your code, the enter/exit logic still fires. Although with your constructor, I can't pass it a parent or text directly.

--
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_m...@googlegroups.com.

osm...@gmail.com

unread,
Aug 19, 2018, 10:34:18 PM8/19/18
to Python Programming for Autodesk Maya
I can send you the whole code to your email, yes.

Sorry, what do you mean with my constructor?how would be the correct way?

osm...@gmail.com

unread,
Aug 19, 2018, 10:40:06 PM8/19/18
to Python Programming for Autodesk Maya
here is the code:

https://pastebin.com/tii0rR1M

thanks again.

Justin Israel

unread,
Aug 20, 2018, 7:15:02 AM8/20/18
to python_in...@googlegroups.com
Ok now I see your problem. Thanks for providing a more complete example. 
You have defined your custom event logic on your CustomButtom class, but then you have your createWindowsFunction() create a local instance which you don't save, and then you call buttonCreator() which creates a completely generic button parented to the Maya main window. That function doesn't return anything so you just end up setting self.windowNorthButton to None. Then you let your local CustomButtom go unsaved and get garbage collected. 
I actually don't even know how this code works correctly but I will only focus on your original question. You aren't operating on your CustomButtom logic. You just use it to create another generic QPushButton. So the solution is to create and save an instance of your custom class instead. 

What I meant about your __init__() is that in its current state it isn't helping you and actually making your CustomButtom less useful by not letting you do CustomButtom("text", main Window). Better to just remove it entirely if it isn't going to do anything useful. 



--
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_m...@googlegroups.com.

Marcus Ottosson

unread,
Aug 20, 2018, 7:42:36 AM8/20/18
to python_in...@googlegroups.com

but I still have no results when my cursor hovers on top of the button:

This works for me.

from PySide import QtGui

class CustomButtom(QtGui.QPushButton):

    def enterEvent(self, event):
        print("Enter")

    def leaveEvent(self, event):
        print("Leave"
)

button = CustomButton()
button.show()
# Enter
# Leave

osm...@gmail.com

unread,
Aug 20, 2018, 3:38:48 PM8/20/18
to Python Programming for Autodesk Maya

Hi Justin. I have 2 questions tho. You mention that I am operating in my custom CustomButtom logic, but when I create a button, I have the text from that custom class. If it`s picking up the default QPushButton class, why I have the text?I think I`m confused about this.

The second thing is that you say create and save. My guess is that I need to create an instance from the CustomButtom class, and return it, which is something I am not doing right now. Is that it?

thanks again for your time, it would take me some time to figure it out by myself.

osm...@gmail.com

unread,
Aug 20, 2018, 3:50:31 PM8/20/18
to Python Programming for Autodesk Maya
@Marcus
Hello Marcus. Thanks for taking some time to answer back.

Yes, this should work with me too, but looks like the problem I have is that I am calling my customButtom class from inside another class, then, when I call the whole thing in maya, it gets garbage collected.

Osmel.

Justin Israel

unread,
Aug 20, 2018, 3:55:48 PM8/20/18
to python_in...@googlegroups.com


On Tue, Aug 21, 2018, 7:38 AM <osm...@gmail.com> wrote:

Hi Justin. I have 2 questions tho. You mention that I am operating in my custom CustomButtom logic, but when I create a button, I have the text from that custom class. If it`s picking up the default QPushButton class, why I have the text?I think I`m confused about this.

First look at this code when you create the button :

    buttonCreatorInstance = CustomButtom()
    self.windowNorth = self.menuLikeWindow("windowNorth")
    self.windowNorthButton = buttonCreatorInstance.buttonCreator(self.windowNorth, "buttonWindNorth",
self.windowsList)

Your custom button is created locally with no text and no parent. It will die after this function ends. 
You call the creator method on it, passing a parent and text. And save None as the result to your windowNorthButton field. 

Now look at how the button is created:

def buttonCreator(self, parentWindow, buttonName, windowsList):
    self.buttonTest = QtGui.QPushButton(parent = parentWindow)
    self.buttonTest.setObjectName(buttonName)
    self.buttonTest.setText("test")

You create your button with just the parent. You set the object name to the text, which isn't a display property. And then you set the text to a hard coded literal. 


The second thing is that you say create and save. My guess is that I need to create an instance from the CustomButtom class, and return it, which is something I am not doing right now. Is that it?

Yes. Instead of that creator method creating a QPushButton, just have it create a CustomButtom, and also don't make it a method of CustomButtom. Honestly, it feels like all of that creator code should be in your CustomButtom.__init__


thanks again for your time, it would take me some time to figure it out by myself.

--
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_m...@googlegroups.com.

osm...@gmail.com

unread,
Aug 20, 2018, 4:11:33 PM8/20/18
to Python Programming for Autodesk Maya
all right. As soon as I get home I`m gonna go line by line with this answer to fix it. Im trying to practice every night, when possible.

Thanks.

osm...@gmail.com

unread,
Aug 24, 2018, 1:58:51 AM8/24/18
to Python Programming for Autodesk Maya
Hi. This is how I fix it, thanks to you guys. I dont know why I was creating a function for the button:

class CustomButtom(QtGui.QPushButton):
def __init__(self, parentWindow, buttonName, windowsList, icon):
super(CustomButtom, self).__init__(parentWindow)

#self.icon = QtGui.QIcon(":/clusterHandle.svg")
QtIcon = QtGui.QIcon(icon)
self.setParent = parentWindow
self.setObjectName(buttonName)
self.setGeometry(QtCore.QRect(0, 0, 50, 50))
self.setIcon(QtIcon)
self.setIconSize(QSize(45, 45))
self.clicked.connect(partial(self.closeWindow, windowsList))
self.setMouseTracking(1)
self.setStyleSheet("border: none;")

thanks again

Justin Israel

unread,
Aug 24, 2018, 3:17:30 AM8/24/18
to python_in...@googlegroups.com
One thing... 
This is a bug:

self.setParent = parentWindow

You replace the actual setParent() method on your button which might cause confusing errors later. There isn't a reason to save that parent since you can query it any time with self.parent()



--
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_m...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages