void MyWidget::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { QPoint position = event->pos(); // Do something with position } }
void MyWidget::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) { QPoint position = event->pos(); // Draw line from previous position to current position } }In this example, the mouseMoveEvent function is overridden to handle mouse drag events. If the left button is held down, the position of the mouse cursor when the event occurs is used to draw a line from the previous position to the current position.