PDA

View Full Version : Drag the Widget



suresh
26th May 2007, 15:19
Hi,

I have QWidget. In that I set setWindowFlags ( Qt::FramelessWindowHint).
I have Image which has designed like the QWidget ie) it has "Maxmize", "Minimize" buttons and Title bar also. I set this Image as my Background.
Now I want to drag or move the Widget using Images Title bar. How can I do it?

marcel
26th May 2007, 15:46
Reimplement the mouse events( press, move, release ) in your widget.
You detect the drag and just use QWidget::move to move the widget. Be careful, you must use QMouseEvent::globalPos() when calculating the new position in order to avoid flicker.

marcel
26th May 2007, 15:53
Something like this:


void FramelessWidget::mousePressEvent( QMouseEvent* e )
{
if( pressed in title bar )
{
mOrigin = e->globalPos(); //mOrigin is a QPoint member
}
}


void FramelessWidget::mouseMoveEvent( QMouseEvent* e )
{
if( e->buttons() & Qt::LeftButton ) //dragging
{
move( pos() + ( e->globslPos() - mOrigin ) );
}
}


I'm not sure this is going to work, so you may have to modify it a bit. :)

marcel
26th May 2007, 15:58
Of course, to avoid all this, you could have created your own style, in which you override everything related to title bars, derived from one of the existing ones.

This way, you wouldn't have had to worry about moving, resizing, how it will behave on other platforms, etc.

suresh
26th May 2007, 16:01
Thanks for your reply. I will try this

jpn
26th May 2007, 16:06
Of course, to avoid all this, you could have created your own style, in which you override everything related to title bars, derived from one of the existing ones.
The title bar of a window is not handled by Qt but the underlying window system. Unfortunately you cannot alter the looks of a title bar with a custom style except for mdi/workspace child windows.