PDA

View Full Version : QGraphicsView::centerOn not working propery in 4.6



deca5423
21st January 2010, 23:46
I've ported a project from 4.5 to 4.6 because I wanted the improved performance for QGraphicsView. However, I noticed that centerOn doesnt appear to work properly, while it was 100% perfect in 4.5. It appears QGraphicsView does some calculation where it tries to show as much of the scene as possible and therefore shows the item I'm trying to centerOn to be in the top left corner of the view. I noticed it never hides any of the item I'm using centerOn with. The item is never centered in the view.




graphicsView->resetTransform();

double ratio = image->width() / (double)scale.width();

mapScene->addItem(pic);

if ( showOffset ) {
mapScene->addLine( offX/ratio, -graphicsView->height(), offX/ratio, graphicsView->height(), QPen(QColor(0,0,0)) );
mapScene->addLine( -graphicsView->width(), offY/ratio, graphicsView->width(), offY/ratio, QPen(QColor(0,0,0)) );
} //makes lines bigger than the view to ensure they take up the whole view, but stops centerOn to work propery in 4.6

graphicsView->centerOn(pic);




QGraphicsView::centerOn works as long as pic is the only item in the view. When the lines are added it's behavior changes when it should be irrelevant. Centering on an item is centering on an item. I'm hoping that I just did something silly that caused it to work in one and not the other... but the code is painfully simple.

wysota
22nd January 2010, 11:13
Or it could be that it was working incorrectly in 4.5 and works fine now :)

What is the alignment of the scene in your view? Did you switch it to Qt::AlignCenter or are you using the default alignment to top-left corner?

deca5423
22nd January 2010, 12:43
Nope, default is center according to the api. The alignment also isn't associated with QGraphicsScene, it's with QGraphicsView:

"For example, if the alignment is Qt::AlignCenter, which is default, the scene will be centered in the view", but this apparently only holds true "If the whole scene is visible in the view", which it is not when the offset lines are drawn.

If you look at the code with the commented section... I draw a picture (QImage) and I display two lines that show x and y offsets associated with the picture. If I don't draw those lines centerOn works fine. Otherwise the picture gets crammed in the top left corner. I'll try specifically setting the alignment to center, even though both the 4.5 and 4.6 state the default is Qt::AlignCenter and I'm using default. I'll try it when I get home but I can't imagine this being the problem.

wysota
22nd January 2010, 13:35
The alignment also isn't associated with QGraphicsScene, it's with QGraphicsView:
I never said it was associated with the scene.


If you look at the code with the commented section... I draw a picture (QImage) and I display two lines that show x and y offsets associated with the picture. If I don't draw those lines centerOn works fine. Otherwise the picture gets crammed in the top left corner. I'll try specifically setting the alignment to center, even though both the 4.5 and 4.6 state the default is Qt::AlignCenter and I'm using default. I'll try it when I get home but I can't imagine this being the problem.

Could you provide a minimal compilable example reproducing the problem?

dchow
28th January 2010, 00:16
I too was having some problems with QGraphicsView::centerOn... so I tried using QGraphicsView::fitInView. This turned out to work really great for my needs. Although, to use it you have specify the exact rectangle that you'd like to see in the View (or the QGraphicsItem), so if you want to see more than just the QGraphicsItem then you need to make sure the rectangle you specify is larger than that item.

Another thing you could do, if your graphical item is always the same size, is to use fitInView to set the View on the graphical item, then use scale to zoom out some amount.

Lastly, if you don't want to change the scale of your view, then don't use fitInView, since it will adjust the transformation matrix (and zoom in/out accordingly).

Good Luck!