0

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> 
1
  • Please be more careful with formatting and indentation, and always check both the preview and the published result. Commented May 19, 2021 at 1:45

1 Answer 1

1

If you want to detect when it is clicked then you must use an eventfilter, you should not assign a function to a method as that method may fail.

import sys from PyQt5 import QtWidgets, uic, QtCore, QtGui class Ui(QtWidgets.QMainWindow): def __init__(self): super(Ui, self).__init__() uic.loadUi("stupid.ui", self) self.show() self.widget.installEventFilter(self) def eventFilter(self, obj, event): if self.widget is obj: if event.type() == QtCore.QEvent.MouseButtonPress: print(self.widget, "press") elif event.type() == QtCore.QEvent.MouseButtonRelease: print(self.widget, "release") return super(Ui, self).eventFilter(obj, event) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) window = Ui() window.setWindowTitle("Mouse Position GUI") app.exec_() `` 
Sign up to request clarification or add additional context in comments.

5 Comments

It was showing errors, then I changed self.widget.installEventFilter(self.widget). Even now nothing is happening.
@user3141066 What error do you get? I have tested it and it works correctly, do not add more code to my code, if you add it it is no longer my responsibility
I get this error: TypeError: super() takes at least 1 argument (0 given)
@user3141066 If you don't provide python version information then I assume you are using the latest version, and obviously the syntax can change from version to version. Try my updated code
No idea what magic you did, but now it works perfectly. Thanks a lot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.