there are widgets in my app which are derived from a custom widget in my app. now i want the widgets not to move beyond the visible screen area.because if it moves beyond that the user will not be able to drag it back and close it through close button.

how to achieve that. I tried to do that as in following code in the event filter of the custom widget:

Qt Code:
  1. //check that the new position is with in available range
  2. QDesktopWidget *desktop = QApplication::desktop();
  3. QRect desktopRect = desktop->availableGeometry();
  4.  
  5. if(desktopRect.contains(keyEvent->globalPos())){
  6. this->move(this->pos() + (keyEvent->globalPos() - mLastMousePosition));
  7. mLastMousePosition = keyEvent->globalPos();
  8. return true;
  9. }
To copy to clipboard, switch view to plain text mode 

This is not working. Where I am doing wrong.
Pls, show me a way to acheive this.

Thanks.