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_())
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_())