Results 1 to 5 of 5

Thread: QGraphicsView::centerOn not working propery in 4.6

  1. #1

    Default QGraphicsView::centerOn not working propery in 4.6

    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.

    Qt Code:
    1. graphicsView->resetTransform();
    2.  
    3. double ratio = image->width() / (double)scale.width();
    4.  
    5. mapScene->addItem(pic);
    6.  
    7. if ( showOffset ) {
    8. mapScene->addLine( offX/ratio, -graphicsView->height(), offX/ratio, graphicsView->height(), QPen(QColor(0,0,0)) );
    9. mapScene->addLine( -graphicsView->width(), offY/ratio, graphicsView->width(), offY/ratio, QPen(QColor(0,0,0)) );
    10. } //makes lines bigger than the view to ensure they take up the whole view, but stops centerOn to work propery in 4.6
    11.  
    12. graphicsView->centerOn(pic);
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView::centerOn not working propery in 4.6

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3

    Default Re: QGraphicsView::centerOn not working propery in 4.6

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView::centerOn not working propery in 4.6

    Quote Originally Posted by deca5423 View Post
    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2010
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView::centerOn not working propery in 4.6

    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!

Similar Threads

  1. beginInsertRows seems not to be working
    By ugurkoltuk in forum Qt Programming
    Replies: 3
    Last Post: 6th January 2010, 14:21
  2. working with shapefiles in Qwt
    By swamyonline in forum Qwt
    Replies: 2
    Last Post: 17th April 2009, 12:08
  3. F1 and WinHelp working with Qt
    By chrisdsimpson in forum Newbie
    Replies: 2
    Last Post: 18th April 2008, 19:08
  4. Propery capable QGraphicsTextItem
    By Gopala Krishna in forum Qt Programming
    Replies: 4
    Last Post: 16th March 2007, 18:26
  5. Mac OS X UI not working
    By hvengel in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2006, 01:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.