Skip to content Skip to sidebar Skip to footer

Using Pyqt4, How Do You Set A Mousemoveevent To Only Work Inside Of A Qwidget In A Qmainwindow But Not In The Mainwindow

My current code below works for updating the x-y coordinates in 2 textBrowsers in my MainWindow, but it doesn't work when the cursor is inside of the textBrowsers. For this example

Solution 1:

From your code example, it looks like you may have already tried an event-filter, but that is probably the best solution. The trick is to install it on the viewport of the widget (if it has one):

class MyMainScreen(QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.textBrowser_1.setMouseTracking(True)
        self.ui.textBrowser_1.viewport().installEventFilter(self)

    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.MouseMove:
            self.ui.textBrowser_1.setText(str(event.x()))
            self.ui.textBrowser_2.setText(str(event.y()))
        return QtGui.QMainWindow.eventFilter(self, source, event)

Post a Comment for "Using Pyqt4, How Do You Set A Mousemoveevent To Only Work Inside Of A Qwidget In A Qmainwindow But Not In The Mainwindow"