4

I want to do hover. I saw an example and then write a script which will be use as I made program. I am facing one problem that hover only occur if you put mouse on the left corner of button. I want that it will happen for all the button that if i move cursor on button then it should change.

Here is my code:

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import pyqtSignal
import os,sys

class HoverButton(QtGui.QToolButton):
    def enterEvent(self,event):
        print("Enter")
        button.setStyleSheet("background-color:#45b545;")

    def leaveEvent(self,event):
        button.setStyleSheet("background-color:yellow;")
        print("Leave")

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
button = QtGui.QToolButton(widget)
button.setMouseTracking(True)
buttonss =  HoverButton(button)
button.setIconSize(QtCore.QSize(200,200))
widget.show()
sys.exit(app.exec_())
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Uahmed
  • 1,847
  • 5
  • 29
  • 45

3 Answers3

9

Is this what you're looking for

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import pyqtSignal
import os,sys


class Main(QtGui.QWidget):

    def __init__(self, parent=None):
        super(Main, self).__init__(parent)

        layout = QtGui.QVBoxLayout(self) # layout of main widget

        button =  HoverButton(self) 
        button.setIconSize(QtCore.QSize(200,200))

        layout.addWidget(button) # set your button to the widgets layout
                                 # this will size the button nicely


class HoverButton(QtGui.QToolButton):

    def __init__(self, parent=None):
        super(HoverButton, self).__init__(parent)
        self.setMouseTracking(True)

    def enterEvent(self,event):
        print("Enter")
        self.setStyleSheet("background-color:#45b545;")

    def leaveEvent(self,event):
        self.setStyleSheet("background-color:yellow;")
        print("Leave")

app = QtGui.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())

In your code you had a button in a button and the nested button wasn't assigned to a QLayout widget. Although, I'm not sure why you're adding a button inside of a button. One thing that I've learned from working with GUI's is that it's so much easier if you modularize your code. Now you can take this custom button and apply it somewhere else.

Jeff
  • 6,932
  • 7
  • 42
  • 72
3

You should use the stylesheet as

QToolButton:hover
{
        background-color: rgb(175,175,175);
}
bostonsqd
  • 96
  • 1
  • 6
0

You probably want focus and blur, rather than enter and leave will only fire when the mouse actually enters or leaves the boundary of the button and will probably just be short duration impulses rather than toggles. focus and blur will toggle with the hover.

synthesizerpatel
  • 27,321
  • 5
  • 74
  • 91
  • i want to change the colour of button as it mouse come on it – Uahmed Feb 22 '12 at 21:32
  • You can do this much easier by utilizing QSS stylesheets and applying a different style for the `:hover` property. Check out http://developer.qt.nokia.com/doc/qt-4.8/stylesheet.html and http://developer.qt.nokia.com/doc/qt-4.8/stylesheet-reference.html – synthesizerpatel Feb 23 '12 at 00:48
  • thanks for the reply but how exactly i can use that i tried .setStyleSheet("hover:blue;") but it didnt work for me – Uahmed Feb 23 '12 at 01:04