PDA

View Full Version : tracking mouse coordinates



lightning2911
28th April 2010, 11:21
I have started using PyQt4 on Windows, done some tutorials and working through the book "Rapid GUI programming with Python and Qt".

I now have a question regarding tracking the mouse coordinates in an application. When I run the code below the status bar shows mouse coordinates at the time of init. But after that there is no updating.

Any tips what I am doing wrong? Also would this be possible via a connect of SIGNAL and SLOT of some sort?

Thanks in advance
Chris


from PyQt4.QtCore import *
from PyQt4.QtGui import *

SCREEN_BORDER = 100

class GraphicsView(QGraphicsView):
def __init__(self, parent=None):
super(GraphicsView, self).__init__(parent)


class MouseCoordinates(QLabel):

def __init__(self, parent=None):
super(MouseCoordinates, self).__init__(parent)

self.update()

def update(self):

currentPos = QCursor.pos()

x = currentPos.x()
y = currentPos.y()

self.setText(" Mouse: %d / %d " % (x, y))

class StatusBar(QStatusBar):

def __init__(self, parent=None):
super(StatusBar, self).__init__(parent)

self.mouseCoords = MouseCoordinates()

self.addWidget(self.mouseCoords)

self.update()

def update(self):

self.mouseCoords.update()


class MainWindow(QMainWindow):

def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)

self.scene = QGraphicsScene(self)
self.scene.setSceneRect(QRectF(0, 0, 800, 600))

# draw border
self.scene.addRect(QRectF(0, 0, 800, 600),
QPen(Qt.darkGray, 1, Qt.DotLine),
QBrush(Qt.lightGray))

# save the current rect as parent object (canvas) for drawing
self.canvas = self.scene.items()[0]


# add view
self.view = GraphicsView()
self.view.setScene(self.scene)

self.status = StatusBar()
if self.status.isSizeGripEnabled():
self.status.setSizeGripEnabled(False)

self.setStatusBar(self.status)

self.setCentralWidget(self.view)


def mouseMoveEvent(self, event):
self.status.update()
super(MainWindow, self).mouseMoveEvent(event)

if __name__ == "__main__":

import sys

# setup application object
app = QApplication(sys.argv)

# create (parent) main window
mainWindow = MainWindow()
rect = QApplication.desktop().availableGeometry()
mainWindow.setGeometry(rect.x() + SCREEN_BORDER,
rect.y() + SCREEN_BORDER,
rect.width() - 2 * SCREEN_BORDER,
rect.height() - 2 * SCREEN_BORDER)
mainWindow.setMinimumSize(900, 700)
mainWindow.setWindowIcon(QIcon("Icon.bmp"))
mainWindow.setWindowTitle("DesignerTest")
mainWindow.show()

# run application object
sys.exit(app.exec_())

psih128
28th April 2010, 11:34
I think this is because of the mouse tracking is disabled for your MainWindow class. Basically Qt wont send any mouse event to the all the widgets, unless they track the mouse events, or have the mouse capture (I might be wrong). Try to put this in your constructor:

self.setMouseTracking(True)

Also consult with the documentation about the mouse tracking for more information.

lightning2911
28th April 2010, 11:44
thanks for the tip but as far as I understand this only changes from only emitting mouseMove events while a button is clicked to always emitting mouseMove events. but unfortunately it does not have any effect here. the problem is the same with or without mouseTracking set.

psih128
28th April 2010, 12:18
Does it update the coordinates when you click the mouse button on your MainWindow?

lightning2911
28th April 2010, 12:49
no, it only updates the coordinates in the init of the statusbar and then nothing.

psih128
28th April 2010, 13:11
i think something is wrong with your mouseMoveHandler. Maybe there is some Py-Qt trick to get it working? Were you able to handle any other events with Py-Qt?

lightning2911
28th April 2010, 13:19
yes, i have tried lots of other demos and even some code i have written myself. that worked fine. but there are so many little details that it is quite easy for me to miss something :-)

i was guessing that the mouseMove eventHandler belongs into some other function. in the demo that i used as example for this there is a wheelEvent in the GraphicsView class. but when i move the mouseMove eventHandler to there it still does not work. and also i did not have access to the statusbar object then ...

i will just keep on reading and experimenting and hope a solution will come my way ... one way or the other :-)

lightning2911
29th April 2010, 12:15
it works now! i misunderstood the way setMouseTracking works. I thought it was a global thing but once I have set it to True on all required widgets it works fine. i also found that it did work without with the mouse button pressing. dont ask me why i did not see that working yesterday. long day i guess ...

thanks for the help and patience

cea
12th December 2011, 00:51
Hi there

I am also having this problem but still can't seem to work out the set tracking. Would you mind posting the version of your code you got working so i could have a look to understand what i am doing wrong.

Cheers