0

I am using a PyQt4.QMainWindow as my application interface, and I want to get the x and y coordinates of the mouse inside of a QWidget and set them continuously in 2 textBrowsers in the MainWindow.

The documentation for QWidget is here.

and the documentation for QMouseEvent is here.

Here is the code

from PyQt4 import QtGui
from PyQt4.QtGui import QApplication
import sys

class Ui_MainWindow(object):
   def setupUI(self, MainWindow):
      self.textBrowser_1 = QtGui.QTextBrowser(self.tab)
      self.textBrowser_2 = QtGui.QTextBrowser(self.tab)
      self.widget_1 = QtGui.QWidget(self.tab)
      self.widget_1.setMouseTracking(True)

class MyMainScreen(QMainWindow):
def __init__(self, parent=None):
    QtGui.QMainWindow.__init__(self, parent)
    self.ui = Ui_MainWindow()   # This is from a python export from QtDesigner
    #   There is a QWidget inside that is self.ui.widget_1
    #   and 2 textBrowsers, textBrowser_1 and textBrowser_2
    #   I want to populate these 2 textBrowsers with the current x,y 
    #   coordinates.

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainscreen = MyMainScreen()           
    mainscreen.show()
    app.exec_()
iehrlich
  • 3,572
  • 4
  • 34
  • 43
Zach G
  • 597
  • 2
  • 10
  • 23
  • You seem to not know very much about Qt. MouseMoveEvent is an event call back that is called from the framework whenever the mouse moves within the widget. This is probably what you want to have, but instead of calling it (doesn't make sense) you probably want to override it and implement your own actions there. – NoDataDumpNoContribution Nov 29 '16 at 09:53
  • Possible duplicate of [Using PyQt4, how do you set a mouseMoveEvent to only work inside of a QWidget in a QMainWindow but not in the MainWindow](http://stackoverflow.com/questions/40878157/using-pyqt4-how-do-you-set-a-mousemoveevent-to-only-work-inside-of-a-qwidget-in) – Zach G Dec 01 '16 at 23:09

1 Answers1

1

When you apply setMouseTracking it only applies to that widget, and not to your children, so you must manually, in the next solution:

def setMouseTracking(self, flag):
    def recursive_set(parent):
        for child in parent.findChildren(QtCore.QWidget):
            child.setMouseTracking(flag)
            recursive_set(child)
    QtGui.QWidget.setMouseTracking(self, flag)
    recursive_set(self)

complete code:

from PyQt4 import QtCore

from PyQt4 import QtGui
from PyQt4.QtGui import QApplication, QMainWindow
import sys


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.resize(800, 132)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
        self.textBrowser_1 = QtGui.QTextBrowser(self.centralwidget)
        self.horizontalLayout.addWidget(self.textBrowser_1)
        self.textBrowser_2 = QtGui.QTextBrowser(self.centralwidget)
        self.horizontalLayout.addWidget(self.textBrowser_2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        MainWindow.setStatusBar(self.statusbar)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


class MyMainScreen(QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.ui = Ui_MainWindow()  # This is from a python export from QtDesigner
        self.ui.setupUi(self)
        self.setMouseTracking(True)
        self.ui.textBrowser_1.setMouseTracking(True)
        self.ui.textBrowser_2.setMouseTracking(True)
        self.ui.menubar.setMouseTracking(True)
        self.ui.statusbar.setMouseTracking(True)

    def setMouseTracking(self, flag):
        def recursive_set(parent):
            for child in parent.findChildren(QtCore.QWidget):
                child.setMouseTracking(flag)
                recursive_set(child)
        QtGui.QWidget.setMouseTracking(self, flag)
        recursive_set(self)

    def mouseMoveEvent(self, event):
        pos = event.pos()
        self.ui.textBrowser_1.append(str(pos.x()))
        self.ui.textBrowser_2.append(str(pos.y()))
        QtGui.QMainWindow.mouseMoveEvent(self, event)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainscreen = MyMainScreen()
    mainscreen.show()
    app.exec_()

This is my output:

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/129372/discussion-on-answer-by-eyllanesc-using-pyqt4-qtgui-qmouseevent-in-a-qwidget). – Bhargav Rao Nov 29 '16 at 19:24