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

Qt Code:
  1. from PyQt4.QtCore import *
  2. from PyQt4.QtGui import *
  3.  
  4. SCREEN_BORDER = 100
  5.  
  6. class GraphicsView(QGraphicsView):
  7. def __init__(self, parent=None):
  8. super(GraphicsView, self).__init__(parent)
  9.  
  10.  
  11. class MouseCoordinates(QLabel):
  12.  
  13. def __init__(self, parent=None):
  14. super(MouseCoordinates, self).__init__(parent)
  15.  
  16. self.update()
  17.  
  18. def update(self):
  19.  
  20. currentPos = QCursor.pos()
  21.  
  22. x = currentPos.x()
  23. y = currentPos.y()
  24.  
  25. self.setText(" Mouse: %d / %d " % (x, y))
  26.  
  27. class StatusBar(QStatusBar):
  28.  
  29. def __init__(self, parent=None):
  30. super(StatusBar, self).__init__(parent)
  31.  
  32. self.mouseCoords = MouseCoordinates()
  33.  
  34. self.addWidget(self.mouseCoords)
  35.  
  36. self.update()
  37.  
  38. def update(self):
  39.  
  40. self.mouseCoords.update()
  41.  
  42.  
  43. class MainWindow(QMainWindow):
  44.  
  45. def __init__(self, parent=None):
  46. super(MainWindow, self).__init__(parent)
  47.  
  48. self.scene = QGraphicsScene(self)
  49. self.scene.setSceneRect(QRectF(0, 0, 800, 600))
  50.  
  51. # draw border
  52. self.scene.addRect(QRectF(0, 0, 800, 600),
  53. QPen(Qt.darkGray, 1, Qt.DotLine),
  54. QBrush(Qt.lightGray))
  55.  
  56. # save the current rect as parent object (canvas) for drawing
  57. self.canvas = self.scene.items()[0]
  58.  
  59.  
  60. # add view
  61. self.view = GraphicsView()
  62. self.view.setScene(self.scene)
  63.  
  64. self.status = StatusBar()
  65. if self.status.isSizeGripEnabled():
  66. self.status.setSizeGripEnabled(False)
  67.  
  68. self.setStatusBar(self.status)
  69.  
  70. self.setCentralWidget(self.view)
  71.  
  72.  
  73. def mouseMoveEvent(self, event):
  74. self.status.update()
  75. super(MainWindow, self).mouseMoveEvent(event)
  76.  
  77. if __name__ == "__main__":
  78.  
  79. import sys
  80.  
  81. # setup application object
  82. app = QApplication(sys.argv)
  83.  
  84. # create (parent) main window
  85. mainWindow = MainWindow()
  86. rect = QApplication.desktop().availableGeometry()
  87. mainWindow.setGeometry(rect.x() + SCREEN_BORDER,
  88. rect.y() + SCREEN_BORDER,
  89. rect.width() - 2 * SCREEN_BORDER,
  90. rect.height() - 2 * SCREEN_BORDER)
  91. mainWindow.setMinimumSize(900, 700)
  92. mainWindow.setWindowIcon(QIcon("Icon.bmp"))
  93. mainWindow.setWindowTitle("DesignerTest")
  94. mainWindow.show()
  95.  
  96. # run application object
  97. sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode