Results 1 to 9 of 9

Thread: tracking mouse coordinates

  1. #1
    Join Date
    Apr 2010
    Posts
    16
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default tracking mouse coordinates

    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 

  2. #2
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: tracking mouse coordinates

    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:
    Qt Code:
    1. self.setMouseTracking(True)
    To copy to clipboard, switch view to plain text mode 

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

  3. #3
    Join Date
    Apr 2010
    Posts
    16
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Re: tracking mouse coordinates

    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.

  4. #4
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: tracking mouse coordinates

    Does it update the coordinates when you click the mouse button on your MainWindow?

  5. #5
    Join Date
    Apr 2010
    Posts
    16
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Re: tracking mouse coordinates

    no, it only updates the coordinates in the init of the statusbar and then nothing.

  6. #6
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: tracking mouse coordinates

    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?

  7. #7
    Join Date
    Apr 2010
    Posts
    16
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Re: tracking mouse coordinates

    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 :-)

  8. #8
    Join Date
    Apr 2010
    Posts
    16
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Re: tracking mouse coordinates

    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

  9. #9
    Join Date
    Dec 2011
    Posts
    1
    Qt products
    Platforms
    Unix/X11

    Default Re: tracking mouse coordinates

    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

Similar Threads

  1. mouse tracking on image
    By vermarajeev in forum Qt Programming
    Replies: 14
    Last Post: 12th May 2010, 13:06
  2. Mouse tracking outside the application interface
    By sophister in forum Qt Programming
    Replies: 7
    Last Post: 2nd May 2009, 06:44
  3. mouse tracking in QGraphicsItem
    By christina123y in forum Qt Programming
    Replies: 10
    Last Post: 9th March 2009, 08:23
  4. Mouse Tracking doesnt work from QCanvasView?
    By hgsw in forum Qt Programming
    Replies: 1
    Last Post: 4th January 2007, 01:59
  5. [QT3+XP] transparency and mouse tracking
    By incapacitant in forum Newbie
    Replies: 9
    Last Post: 17th February 2006, 18:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.