I am trying to get my mouse position on a Widget when clicked. There seem to various methods to do that. The underMouse function seems to be the most popular one. I do not know what I am doing wrong. I tried all the possible techniques. Below is my python code, other than this I have a ui file.
from PyQt5 import QtWidgets, uic, QtCore, QtGui from PyQt5.QtWidgets import * from PyQt5.QtGui import QPixmap, QMovie import pyqtgraph as qwt_plot import sys class Ui(QtWidgets.QMainWindow): def __init__(self): super(Ui, self).__init__() # Call the inherited classes __init__ method uic.loadUi('stupid.ui', self) # Load the .ui file self.show() # Show the GUI self.frame = None self.timer = QtCore.QTimer(self) self.timer.timeout.connect(self.run) self.track = QtGui.QWidget(self.widget) self.setMouseTracking(True) self.track.mouseReleaseEvent = self.gig def gig(self, event): print ("Method 1 worked") def mouseReleaseEvent(self, event): posMouse = event.pos() #print("%d, %d" % (posMouse.x(), posMouse.y())) #print (self.track.geometry()) if self.track.geometry().contains(posMouse): print "Under Mouse" if self.track.underMouse(): print ("phew") def run(self): # Do nothing if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) # Create an instance of QtWidgets.QApplication window = Ui() # Create an instance of our class window.setWindowTitle('Mouse Position GUI') app.exec_() # Start the application The ui file:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>932</width> <height>546</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <property name="windowIcon"> <iconset> <normaloff>icon.png</normaloff>icon.png</iconset> </property> <widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="widget" native="true"> <property name="geometry"> <rect> <x>110</x> <y>0</y> <width>631</width> <height>511</height> </rect> </property> </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>932</width> <height>22</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/> </ui>