PyQt4 is a Python binding of the cross-platform GUI toolkit Qt, developed by Digia PLC. The QtGui module provides a set of classes for GUI applications that use Qt widgets, such as buttons, labels, and text boxes. QCursor is a class under the QtGui module that provides a graphical image for the mouse pointer in a GUI application. Here are some examples of how to use QCursor:
Example 1: Changing the mouse pointer icon
import sys from PyQt4.QtGui import QApplication, QCursor
app = QApplication(sys.argv) cursor = QCursor() cursor.setShape(Qt.PointingHandCursor) # Change the cursor icon to a pointing hand app.setOverrideCursor(cursor) # Set the new cursor as the current cursor
# Do some work here...
app.restoreOverrideCursor() # Restore the previous cursor
This code sets a new cursor icon for the mouse pointer using QCursor.setShape() and temporarily overrides the system cursor using PyQt4.QtGui.QApplication.setOverrideCursor(). The previous cursor is then restored using PyQt4.QtGui.QApplication.restoreOverrideCursor().
Example 2: Obtaining the position of the mouse pointer
import sys from PyQt4.QtGui import QApplication, QWidget
cursor_pos = widget.mapFromGlobal(QCursor.pos()) # Get the position of the mouse pointer relative to the widget print(f"The mouse pointer is at ({cursor_pos.x()}, {cursor_pos.y()})")
In this example, we create a QWidget and show it, then obtain the position of the mouse pointer relative to the QWidget using QWidget.mapFromGlobal() and QCursor.pos(). We then print out the X and Y coordinates of the mouse pointer.
Both examples use PyQt4.QtGui as the package library.
Python QtGui.QCursor - 30 examples found. These are the top rated real world Python examples of PyQt4.QtGui.QCursor extracted from open source projects. You can rate examples to help us improve the quality of examples.