Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, April 2008
Moving a widget within a frame


Message 1 in thread

Hi

I'm (still) trying to move a widget within a frame. I'm getting as far as 
being able to move it, but it is not quite right.

The 'obvious' thing to try seems to be:

void MyWidget::mouseMoveEvent ( QMouseEvent *event )
{
	if ( m_Moving ) move( event->globalPos()  );
}

As soon as I start to move the widget, it jumps to an offset that is 
equivalent to the global co-ords of the parent frame.

If I try subtracting this offset using something like

 mapToGlobal( dynamic_cast<QWidget*>(parent())->pos() )

Then I get a second copy of the widget flickering near the top left corner of 
the frame. As I move the mouse it gets further and further away from the 
original widget.

I've tried just about every combination of mapTo, mapToParent, mapToGlobal, 
etc. with every conceivable control in my app, but nothing seems to get it to 
just move the widget as you would expect.

Nick

--
 [ signature omitted ] 

Message 2 in thread

Am Montag, 21. April 2008 schrieb Nicholas Robinson:
> I'm (still) trying to move a widget within a frame. I'm getting as far as
> being able to move it, but it is not quite right.
> The 'obvious' thing to try seems to be:
> void MyWidget::mouseMoveEvent ( QMouseEvent *event )
> {
> 	if ( m_Moving ) move( event->globalPos()  );
> }
> As soon as I start to move the widget, it jumps to an offset that is
> equivalent to the global co-ords of the parent frame.
> I've tried just about every combination of mapTo, mapToParent, mapToGlobal,
> etc. with every conceivable control in my app, but nothing seems to get it
> to just move the widget as you would expect.

Does the parent of MyWidget receive the mouseMoveEvents when MyWidget is not 
handling them? The you would just have to check wether event->pos() is inside 
MyWidget (probably with MyWidget->rect()->intersects( event->pos() ) or 
something like that) and then do the moves...

Maybe that helps,

Arnold
-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 3 in thread

On Mon, Apr 21, 2008 at 5:41 PM, Nicholas Robinson <npr@xxxxxxxxxxxxxxxx>
wrote:

> Hi
>
> I'm (still) trying to move a widget within a frame. I'm getting as far as
> being able to move it, but it is not quite right.
>

This should be pretty simple, you just have to keep track of all of the
coordinate systems.


>
> The 'obvious' thing to try seems to be:
>
> void MyWidget::mouseMoveEvent ( QMouseEvent *event )
> {
>        if ( m_Moving ) move( event->globalPos()  );
> }
>

The mouse event for your widget gives you the position in your widget's
coordinate system (QMouseEvent::pos()), and the global coordinate system
(QMouseEvent::globalPos()).

QWidget::move() expects a point in the parent widget's coordinate system.
From your widget, you can call parentWidget()->mapFromGlobal() to convert a
point in the global coordinate system into a point in the parent widget's
coordinate system.

Hope that helps,
Tom

Message 4 in thread

On Tuesday 22 April 2008 13:52:40 Tom Panning wrote:
> On Mon, Apr 21, 2008 at 5:41 PM, Nicholas Robinson <npr@xxxxxxxxxxxxxxxx>
>
> wrote:
> > Hi
> >
> > I'm (still) trying to move a widget within a frame. I'm getting as far as
> > being able to move it, but it is not quite right.
>
> This should be pretty simple, you just have to keep track of all of the
> coordinate systems.
>
> > The 'obvious' thing to try seems to be:
> >
> > void MyWidget::mouseMoveEvent ( QMouseEvent *event )
> > {
> >        if ( m_Moving ) move( event->globalPos()  );
> > }
>
> The mouse event for your widget gives you the position in your widget's
> coordinate system (QMouseEvent::pos()), and the global coordinate system
> (QMouseEvent::globalPos()).
>
> QWidget::move() expects a point in the parent widget's coordinate system.
>
> >From your widget, you can call parentWidget()->mapFromGlobal() to convert
> > a
>
> point in the global coordinate system into a point in the parent widget's
> coordinate system.
>
> Hope that helps,
> Tom

Hi

Thanks to both you and Arnold for your replies. I'm a lot further down the 
road now!

I now have a smoothly moving widget with no flickering - so a big improvement. 
The only thing left is that if I click my mouse somewhere within the widget, 
when I start to move it, the widget jumps so that the top left corner of the 
widget is under the mouse and then it moves to the final release point. It 
would be nice if it just moved with the mouse still over the same relative 
position within the widget.

I thought it would be something simple like allowing for the event-pos() 
coordinates within the widget, but it seems it is a bit more complicated than 
that!

Thanks again

Nick

--
 [ signature omitted ] 

Message 5 in thread

Am Dienstag, 22. April 2008 schrieb Nicholas Robinson:
> I now have a smoothly moving widget with no flickering - so a big
> improvement. The only thing left is that if I click my mouse somewhere
> within the widget, when I start to move it, the widget jumps so that the
> top left corner of the widget is under the mouse and then it moves to the
> final release point. It would be nice if it just moved with the mouse still
> over the same relative position within the widget.
> I thought it would be something simple like allowing for the event-pos()
> coordinates within the widget, but it seems it is a bit more complicated
> than that!

I think you have to save the initial event->pos() and then move MyWidget 
relative...

Have fun,

Arnold
-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 6 in thread

On Wednesday 23 April 2008 09:01:07 Arnold Krille wrote:
> Am Dienstag, 22. April 2008 schrieb Nicholas Robinson:
> > I now have a smoothly moving widget with no flickering - so a big
> > improvement. The only thing left is that if I click my mouse somewhere
> > within the widget, when I start to move it, the widget jumps so that the
> > top left corner of the widget is under the mouse and then it moves to the
> > final release point. It would be nice if it just moved with the mouse
> > still over the same relative position within the widget.
> > I thought it would be something simple like allowing for the event-pos()
> > coordinates within the widget, but it seems it is a bit more complicated
> > than that!
>
> I think you have to save the initial event->pos() and then move MyWidget
> relative...
>
> Have fun,
>
> Arnold

Arnold

Thanks, you were spot on again!

Nick

--
 [ signature omitted ]