PDA

View Full Version : Changing co-ordinate system in a QGraphicsView



danbelsey
6th September 2011, 11:48
Hi,

I've made a QGraphicsView and I've added to it a custom graphicsItem. In its paint event it draws a rectangle with top left at (0,0) - but in the QGraphicsView it's always centered, and I want it in the top left. I've tried setting the graphic view's alignment to AlignLeft/AlignTop but that changes nothing. How do I set it so that (0,0) is always the top left corner? I ask now because eventually it'll be much larger; if I add an item at something like (10000, 10000) afterwards it works fine, but centers the view when the scene is smaller!

Thanks in advance!

high_flyer
7th September 2011, 13:01
Be sure to carefully read the QGraphicsViewer, QGraphicsScene and QGraphicsItem documentation.


In its paint event it draws a rectangle with top left at (0,0)
In which coordinate system? I take it you mean on scene coordinates.
From the docs:

Items can be placed at any position on the scene, and the size of the scene is by default unlimited.
As you can see, as the scene is unlimited in size, position 0,0 is just as good as any other, as it is infinite in any direction.
0,0 can't be at the "left upper corner" since there is no limit in both x or y.

What you want is to show a certain part of the scene (such that the item will be seen at the upper left corner of the view.
Again, read in the docs how to do that (show certain part of the scene), ask if you get stuck with specifics.

wysota
7th September 2011, 13:55
How do I set it so that (0,0) is always the top left corner?
You explicitly set the scene to begin at (0,0) by setting its geometry to a rect starting with (0,0). If you wish to retain that configuration when the scene size is smaller than the view size, align the scene to top and left of the view.

danbelsey
7th September 2011, 14:25
Again, read in the docs how to do that (show certain part of the scene), ask if you get stuck with specifics.
This is indeed the part I get stuck with! I've been trying to display a rectangle, starting at the co-ordinates (0,0), and of size (graphicView->size()->width(), graphicView->size()->height() - so that I create a scene the same size as the viewer, with (0,0) at the top left. I've tried:

_ui->graphicsView->ensureVisible(0,0,_ui->graphicsView->size().width(), _ui->graphicsView->size().height(), 0, 0);
_ui->graphicsView->fitInView(0,0,_ui->graphicsView->size().width(), _ui->graphicsView->size().height());

But this appears to change nothing! I've also tried using translate() with various params but the view doesn't move. Where am I going wrong?

Cheers!

wysota
7th September 2011, 14:35
If your scene is smaller than the view then fitInView is a no-op.

high_flyer
7th September 2011, 15:18
If your scene is smaller than the view then fitInView is a no-op.
Actually quite the contrary, you can't have it NOT in the view ;)


_ui->graphicsView->ensureVisible(0,0,_ui->graphicsView->size().width(), _ui->graphicsView->size().height(), 0, 0);
_ui->graphicsView->fitInView(0,0,_ui->graphicsView->size().width(), _ui->graphicsView->size().height());

Try using QGraphicsView::centerOn() and give the center position of your desired rect.

danbelsey
7th September 2011, 15:44
Ah, I found out my problem; working on the wrong object! Setting the alignment on the correct object did, indeed, fix it. I did wonder why that wasn't doing anything in the creator...

Apologies for wasting your time, but thanks for your help :) This is why you should document your design for the person who works after you, I suppose!

wysota
7th September 2011, 16:03
Actually quite the contrary, you can't have it NOT in the view ;)

So calling it is a no-op, it has no effect.

high_flyer
7th September 2011, 16:24
So calling it is a no-op, it has no effect.
I know what you meant, that is why I added the wink smilie.