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

Qt-interest Archive, December 2006
Finding the global coordinates for a child widget


Message 1 in thread

Hi. I'm attempting to determine the global coordinates for a child  
widget.

The method I am using is:

QRect globalRect;
globalRect = QRect(mapToGlobal(glWidget->rect().topLeft()),  
mapToGlobal(glWidget->rect().bottomRight()));

where glWidget is the child widget I'm interested in.

This answer gives me something that is close, but still not right.  
The result is 10 pixels off in both x and y coordinates. glWidget is  
inside a QVBoxLayout.

Any ideas why the 10 pixel discrepancy? I tried looking at the  
geometry() property of the QVBoxLayout. It has xy coordinates of 0,0.  
I also tried examining the parent of glWidget. Calling parentWidget()  
on this widget gives me the window that is the main window of my app.  
So, there isn't some kind of second level sub-parent.

I had also tried the following method:

QRect globalRect;
QRect ir = glWidget->frameGeometry();
QRect mr = mainWidget->window()->frameGeometry();
		
globalRect.setX(mr.x()  + ir.x());
globalRect.setY(mr.y()  + ir.y());
globalRect.setWidth(glWidget->rect().width());
globalRect.setHeight(glWidget->rect().height());

And this also produced results that were 10 pixels off.

I'm using Qt 4.2.2 on MacOS X (10.4.8).

Thanks for any advice.

Brant Sears

--
 [ signature omitted ] 

Message 2 in thread

Brant Sears wrote:
> Hi. I'm attempting to determine the global coordinates for a child widget.
> 
> The method I am using is:
> 
> QRect globalRect;
> globalRect = QRect(mapToGlobal(glWidget->rect().topLeft()),
> mapToGlobal(glWidget->rect().bottomRight()));
> 
> where glWidget is the child widget I'm interested in.
> 
> This answer gives me something that is close, but still not right. The
> result is 10 pixels off in both x and y coordinates. glWidget is inside
> a QVBoxLayout.
> 
> Any ideas why the 10 pixel discrepancy?
> I'm using Qt 4.2.2 on MacOS X (10.4.8).

Did you try

glWidget->mapToGlobal( QPoint( 0, 0 ))
vs.
mapToGlobal( glWidget->rect().topLeft())

I haven't tried, but what it looks like here is the wrong context.
glWidget->rect().topLeft() == QPoint(0,0) because rect() is called from
the child context so its origin is with respect to self. The mapToGlobal
is called from parent (?) widget so it is using the wrong origin when
translating.

- Adam

--
 [ signature omitted ] 

Message 3 in thread

Thank you very much. This change has fixed the problem.


On Dec 20, 2006, at 12:28 PM, Adam Majer wrote:

> Brant Sears wrote:
>> Hi. I'm attempting to determine the global coordinates for a child  
>> widget.
>>
>> The method I am using is:
>>
>> QRect globalRect;
>> globalRect = QRect(mapToGlobal(glWidget->rect().topLeft()),
>> mapToGlobal(glWidget->rect().bottomRight()));
>>
>> where glWidget is the child widget I'm interested in.
>>
>> This answer gives me something that is close, but still not right.  
>> The
>> result is 10 pixels off in both x and y coordinates. glWidget is  
>> inside
>> a QVBoxLayout.
>>
>> Any ideas why the 10 pixel discrepancy?
>> I'm using Qt 4.2.2 on MacOS X (10.4.8).
>
> Did you try
>
> glWidget->mapToGlobal( QPoint( 0, 0 ))
> vs.
> mapToGlobal( glWidget->rect().topLeft())
>
> I haven't tried, but what it looks like here is the wrong context.
> glWidget->rect().topLeft() == QPoint(0,0) because rect() is called  
> from
> the child context so its origin is with respect to self. The  
> mapToGlobal
> is called from parent (?) widget so it is using the wrong origin when
> translating.
>
> - Adam
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx  
> with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>

--
 [ signature omitted ]