void MyWidget::mousePressEvent(QMouseEvent *event) { QPoint globalPos = event->globalPos(); qDebug() << "Mouse click at global position: " << globalPos; }
void MyWidget::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) { QPoint globalPos = event->globalPos(); QPoint delta = globalPos - m_dragStartPosition; move(pos() + delta); } } void MyWidget::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) m_dragStartPosition = event->globalPos() - frameGeometry().topLeft(); }In this example, the widget is moved relative to its current position by calculating the difference between the current global position and the position where the mouse click occurred (stored in m_dragStartPosition). This code requires the use of the Qt library, which includes the QMouseEvent class.