void MyWidget::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { // Handle left mouse button click buttonClicked(); } }
void MyGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { QPointF newPos = event->scenePos() - lastPos; setPos(pos() + newPos); lastPos = event->scenePos(); }This code is part of a QGraphicsItem subclass, which represents a graphical object in a QGraphicsScene. It overrides the mouseMoveEvent method to handle mouse movement events. In this example, the method calculates the distance that the mouse has moved since the last event and uses that to update the position of the graphics item. Package library: Qt Overall, QMouseEvent is a useful class for handling mouse events in Qt applications. It provides a variety of information about the event, allowing developers to easily detect what happened and respond accordingly.