MoveWidget

Christian Couder chcouder at club-internet.fr
Thu Dec 30 10:28:35 GMT 1999


"Rene Märten" wrote:

> Hi all,
>
> what i want to do is:
>
> i want to get a widget, wich is able to auto-move his coordinates if the mousebutton is pressed on it and then i move the mouse...
> is there any function, class etc to do this without 100 lines of own code ?

Hi Rene,

I think you have to implement something like this:


void YourWidget::mousePressEvent(QMouseEvent *e) {

  if (e->button() == LeftButton) {

    widgetRectWhenPressed = geometry();

    xMousePosWhenPressed = e->x();
    yMousePosWhenPressed = e->y();
  }
}

void YourWidget::mousePressEvent(QMouseEvent *e) {

  if (e->state() & LeftButton) {

     move(widgetRectWhenPressed.x() + e->x() -  xMousePosWhenPressed,
                widgetRectWhenPressed.y() + e->y() -  yMousePosWhenPressed);
  }
}


See KWriteView::mouseMoveEvent and KWriteView::mousePressEvent as examples.

Hope this helps,
Christian.





More information about the KDevelop mailing list