[Qt-interest] Unable to get sensible coordinate mappings
David Boddie
david.boddie at nokia.com
Mon May 18 16:07:35 CEST 2009
Andrew Hodgkinson wrote:
> (I asked this on qt-embedded-interest a few days ago but was asked to try
> here instead - sorry if you're on both lists and get this twice!).
>
> I have a QT Embedded 4.5.1 application running on Linux but its behaviour
> with respect to local (item) and scene (device) coordinates is very
> strange. I would appreciate any advice, documentation pointers etc. that
> anyone can offer - I'm new to Qt and may have misunderstood how the
> coordinate system works.
You should take a look at this document if you haven't already seen it:
http://doc.trolltech.com/4.5/coordsys.html
For Graphics View, the coordinate system is explained here:
http://doc.trolltech.com/4.5/graphicsview.html#the-graphics-view-coordinate-system
[...]
> The code will run on a digital TV receiver, so there is no concept of a
> WIMP-style environment in the GUI. Traditional window tools are
> meaningless. Accordingly, the tools and frame are hidden and the view's
> window is resized to fill the screen.
>
> setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
> setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
> setWindowFlags ( Qt::FramelessWindowHint );
> resize ( size );
Out of interest, why don't you call showFullScreen() on the view?
> A QGraphicsScene is constructed.
>
> QRectF bounds( 0, 0, geometry.width(), geometry.height() );
>
> scene = new QGraphicsScene( bounds, this );
> scene->setItemIndexMethod( QGraphicsScene::NoIndex );
> setScene( scene );
>
> So far, so good. I believe I now have an object providing a view onto a
> scene with a coordinate system that is not scaled, so the scene's logical
> coordinates map 1:1 to pixels on the device. The scene covers the whole
> of the device's display area.
Yes, that's correct in theory.
> The QGraphicsView derivative now wants to draw things in the scene it has
> created. It instantiates a derivative of QGraphicsItem and adds it to the
> scene with "scene->addItem". The trouble starts here. The purpose of the
> class is to display a banner across the screen's width at about 1/3 of
> its height. A nominal resolution of 1280x720 is chosen; if the device is
> actually operating in a higher (e.g. 1920x1080) or lower (e.g. 640x480)
> resolution, the UI will scale.
It sounds like you expect it to scale, but does it actually scale?
[...]
> I don't understand this. My scene coordinates are not scaled by the view
> and the QGraphicsItem derivative merely sets its own local coordinate
> scope and a viewport. The viewport, of course, *is* set in scene
> coordinates and debug output shows that these are the expected values
> depending on the device size. So, scene()->sceneRect() is the 'right
> size', but mapping any item rectangle to scene coordinates with e.g.
> mapRectToScene() returns nonsensical results.
I'm not convinced that setting the sceneRect property does what you expect,
and some simple checks also led me to the conclusion that it didn't do what
I expected, either. :-/
[...]
If you're happy with the idea of scaling up from a scene containing items
placed within a 1280x720 rectangle then one thing you might want to try is
to set a transform on the view, mapping scene coordinates to the screen
geometry.
Perhaps something as simple as this will work:
QTransform transform;
transform.scale(geometry.width()/1280.0, geometry.height()/700.0);
view->setTransform(transform);
It won't solve your aspect ratio problems, but perhaps that's something we
can figure out separately.
> [1] You will note that the example implementation of 'boundingRect'
> given in the method documentation is correct, while the example
> given in the 'details' introduction section is incorrect. It adds
> pen width divided by 2 to the width / height - should not divide
> by two there.
Agreed. I'll fix that.
David
--
David Boddie
Senior Technical Writer
Nokia, Qt Software
More information about the Qt-interest-old
mailing list