How can I find out if the mouse is above the widgets?

I am new to Qt (PyQt - PySide).
I am trying to create my own widget, which is a menu. However, I was faced with a difficult road, and it seems that I can not figure out myself. I read the documentation, but I don’t think there is a mouse state that I can check to see if the mouse cursor is over this widget.

I call the function on mouseReleaseEvent QWidget .

Example:.

 def mouseReleaseEvent(self, e): 

When this event fires, I need to know if the mouse is actually above or outside the widgets (the widget from which this event is fired).

  if mouseCursorOverSelf == True: # do something .. 

How can I achieve this? What do I need to do?

Many thanks!

+7
source share
2 answers

If you want to track when a mouse enters or exits a widget, you can use something like this:

 #!/usr/bin/env python #-*- coding:utf-8 -*- from PyQt4 import QtCore, QtGui class mainwindow(QtGui.QWidget): def __init__(self, parent=None): super(mainwindow, self).__init__(parent) def enterEvent(self, event): print "Mouse Entered" return super(mainwindow, self).enterEvent(event) def leaveEvent(self, event): print "Mouse Left" return super(mainwindow, self).enterEvent(event) if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) main = mainwindow() main.show() sys.exit(app.exec_()) 

If you just want to check if the mouse is above the widgets, you can use the QWidget::underMouse () function:

 #!/usr/bin/env python #-*- coding:utf-8 -*- from PyQt4 import QtCore, QtGui class mainwindow(QtGui.QWidget): def __init__(self, parent=None): super(mainwindow, self).__init__(parent) self.button = QtGui.QPushButton("Check Mouse in 3 seconds") self.button.clicked.connect(self.on_button_clicked) self.layout = QtGui.QHBoxLayout(self) self.layout.addWidget(self.button) def mouseReleaseEvent(self, event): if self.underMouse(): print "Do something" return super(mainwindow, self).mouseReleaseEvent(event) @QtCore.pyqtSlot() def on_button_clicked(self): QtCore.QTimer.singleShot(3000, self.checkMouse) def checkMouse(self): print "Under Mouse: {0}".format(self.underMouse()) if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) main = mainwindow() main.show() sys.exit(app.exec_()) 

Another method involves checking whether the mouse position is inside the widget’s internal geometry:

 #!/usr/bin/env python #-*- coding:utf-8 -*- from PyQt4 import QtCore, QtGui class mainwindow(QtGui.QWidget): def __init__(self, parent=None): super(mainwindow, self).__init__(parent) self.setMouseTracking(True) def mouseReleaseEvent(self, event): posMouse = event.pos() if self.rect().contains(posMouse): print "Under Mouse" return super(mainwindow, self).mouseReleaseEvent(event) if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) main = mainwindow() main.show() main.resize(200, 200) sys.exit(app.exec_()) 
+11
source

underMouse () is what should be used to determine if your mouse is above the widgets.
Usage: widget.underMouse()
http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#underMouse

+4
source

All Articles