The default scene-view scale is 1:1 thus one logical unit in scene coordinates corresponds to one pixel in the view coordinates.
Yes, got that. I actually just figured out what I was doing wrong.
scene->setRect( 0, 0, 200, 200 );
// MyGraphicsItem constructor sets initial bounding rect to (0, 0, 1000, 100)
MyGraphicsItem * item = new MyGraphicsItem();
item->setPos( 10, 10 );
// Scale x coordinate only (y coordinate *is* pixels)
QGraphicsScale scale;
scale.setXScale( scene->width() / item->boundingRect().width() );
QList< QGraphicsTransform * > transforms;
transforms.push_back( &scale );
item->setTransformations( transforms );
// ...
QGraphicsScene * scene = new QGraphicsScene();
scene->setRect( 0, 0, 200, 200 );
// MyGraphicsItem constructor sets initial bounding rect to (0, 0, 1000, 100)
MyGraphicsItem * item = new MyGraphicsItem();
item->setPos( 10, 10 );
// Scale x coordinate only (y coordinate *is* pixels)
QGraphicsScale scale;
scale.setXScale( scene->width() / item->boundingRect().width() );
QList< QGraphicsTransform * > transforms;
transforms.push_back( &scale );
item->setTransformations( transforms );
// ...
To copy to clipboard, switch view to plain text mode
I determined that my mistake was that the QGraphicsScale instance can't be created on the stack (or if it is, its lifetime must be at least the same as the QGraphicsItem that uses it). Changing the QGraphicsScale instance to a heap-allocated pointer fixes the problem, and I now get a properly scaled line.
So, I think I'm on my way after 3 days of trying to understand and getting a bloody forehead from pounding it on the desk.
One additional question: Can QGraphicsTransform instances be shared among several items, or are they "owned" by the item after calling setTransformations()?
Bookmarks