from PyQt5.QtCore import QPointF # Creating a QPointF object with coordinates (2.5, -3.0) point = QPointF(2.5, -3.0) # Printing the coordinates of the point print(point.x(), point.y())
from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtCore import Qt, QPointF, QRectF from PyQt5.QtGui import QPainter class MyWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.setFixedSize(300, 300) def paintEvent(self, event): qp = QPainter() qp.begin(self) qp.drawRect(QRectF(10, 10, 280, 280)) qp.end() def mousePressEvent(self, event): if event.button() == Qt.LeftButton: pos = event.pos() point = QPointF(pos.x(), pos.y()) print(f"Clicked on point {point.x()}, {point.y()}") if __name__ == "__main__": app = QApplication([]) widget = MyWidget() widget.show() app.exec_()In this example, we create a custom QWidget (MyWidget) that draws a rectangle in its paintEvent. We also override the mousePressEvent to handle left button clicks. When the user clicks on the widget, the mousePressEvent is triggered and we create a QPointF object with the coordinates of the clicked point. We then print the coordinates to the console. Package/Library: PyQt5