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?

mtb
  • 1,350
  • 16
  • 32

1 Answers1

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 ) );
mtb
  • 1,350
  • 16
  • 32
Timo
  • 1,724
  • 14
  • 36
  • `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. – mtb Jul 18 '16 at 07:29