1

I want the mouse cursor to be in the position where the mouse was clicked on the widget or in the center of the widget.

For example if the widget is a rectangle and it is in drag event it appears like in the image below, event I've clicked in the center and started to drag:

enter image description here

Where is the red circle is the mouse cursor every time, wherever I "pick-up" the widget.

In the mousePressEvent, I do something like:

void myQFrame::mousePressEvent( QMouseEvent* event ) { if( event->button() == Qt::LeftButton ) { QDrag* drag = new QDrag( this ); QMimeData* mimeData = new QMimeData; //....other stuff drag->setMimeData( mimeData ); QPixmap widgetPixmap(this->size()); this->render( &widgetPixmap, QPoint(), QRegion( this->rect() ) ); } } 

Haw can I set the cursor to be in center for example if the widget was picket from the center?

1 Answer 1

1

QDrag::setHotSpot is your friend.

UPDATE:

Sets the position of the hot spot relative to the top-left corner of the pixmap used to the point specified by hotspot.

Note: on X11, the pixmap may not be able to keep up with the mouse movements if the hot spot causes the pixmap to be displayed directly under the cursor.

drag->setHotSpot( QPoint( this->width() / 2, this->height() / 2 ) ); 
Sign up to request clarification or add additional context in comments.

1 Comment

setHotspot is the right function, but it takes a QPoint as parameter not a QSize. So the good solution was: drag->setHotSpot( QPoint( this->width() / 2, this->height() / 2 ) ); Thank you. I will update your answer and accept it as solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.