开发者

PyQTQ HoverEvent on Qpushbutton?

开发者 https://www.devze.com 2023-03-24 05:57 出处:网络
Hi I have been searching for a sample on my problem. i got a Push Button, it is made through codes.i created 10 of them, now my p开发者_运维知识库roblem is i need to set a Hover Event on it. i have be

Hi I have been searching for a sample on my problem. i got a Push Button, it is made through codes.i created 10 of them, now my p开发者_运维知识库roblem is i need to set a Hover Event on it. i have been reading QHoverbut still i cant make the right code. the sample cant be understand by a beginner please help me out. really need to

EDIT: I have search more for some answers regarding this. i found that i need to setup a QEvent.HoverEnter is this correct?


I've subclassed the mouseMoveEvent of a QPushButton before to find out when the mouse is hovering over it. Here's a sample:

from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, \
            QVBoxLayout, QWidget

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

    def mouseMoveEvent(self, event):
        print 'Mouse moved!'

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent) 
        button = HoverButton('Test')
        centralWidget = QWidget()
        vbox = QVBoxLayout(centralWidget)
        vbox.addWidget(button)
        self.setCentralWidget(centralWidget)

def startmain():
    app = QApplication(sys.argv)
    mainwindow = MainWindow()
    mainwindow.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    import sys
    startmain()

You could make your HoverButton object emit a signal and then connect other functions to the signal in your main window.

If you just want to know when the mouse enters and leaves the QPushButton then re-implement the enterEvent and leaveEvent methods instead.


In your window that contains the buttons you can implement you own event filter

class MyWidget(QWebView):
    def __init__(self):
        super(MyWidget, self).__init__()
        self.installEventFilter(self)

    def eventFilter(self, qobject, qevent):
        qtype = qevent.type()
        if qtype == QEvent.HoverMove:
           ... hover logic depending in which qobject, since you have 10 buttons
        return super(MyWidget, self).eventFilter(qobject, qevent)
0

精彩评论

暂无评论...
验证码 换一张
取 消