Results 1 to 3 of 3

Thread: QGraphicsView::centerOn not working in QMainWindow's constructor

  1. #1
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default QGraphicsView::centerOn not working in QMainWindow's constructor

    In my main window's constructor I create a QGraphicsScene and View, set the scene to the view, add the QGraphicsView to the main window's layout, and then call QGraphicsView::centerOn(0,0), so that the QGraphicsView will show the scene in the middle of the view. However, it would display the scene offset by some weird amount, rather than in the middle like I want it to.

    I've heard about some general funk relating to QGraphicsView and having to call it's methods *after* it's drawn onto the screen. Well, that didn't really help, because I was calling QGraphicsView::centerOn(0,0) at the very end of my main window's constructor... so I didn't see how to call it after that, unless I set up a timer or something stupid like that.

    But then I had the idea to re-implement QMainWindow::showEvent, and call QGraphicsView::centerOn(0,0) in there, rather than in the constructor. What do you know... that fixed it!

    My question is: Why does this happen? Am I missing something?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QGraphicsView::centerOn not working in QMainWindow's constructor

    Constructing a QMainWindow object and displaying the window that object represents are two different things. At the very end of the constructor the main window is not visible.

    What size does the undisplayed graphic view have in the undisplayed main window when you ask it to centre the view? When the main window is displayed and gets a concrete size so does the graphics view. How do you expect the graphics view content to move during this resize? Do you expect it to keep the same centre when it is resized during first display, but not when manually resized? The scroll bar granularity can also have an impact.

    It's three lines of code to ensure the scene is centred on first showing:
    Qt Code:
    1. MainWindow(...) {
    2. ...
    3.  
    4. QTimer::singleShot(0, this, SLOT(centreOnFirstDisplay()));
    5. }
    6.  
    7. public slots:
    8. void centreOnFirstDisplay() { view->centerOn(0, 0); }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    wayfaerer (13th March 2012)

  4. #3
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Re: QGraphicsView::centerOn not working in QMainWindow's constructor

    Very helpful. Thank you!

Similar Threads

  1. Replies: 0
    Last Post: 18th October 2010, 02:16
  2. QGraphicsView ensureVisible() and centerOn()
    By onurozcelik in forum Qt Programming
    Replies: 0
    Last Post: 25th May 2010, 15:07
  3. Replies: 3
    Last Post: 25th May 2010, 11:29
  4. QGraphicsView::centerOn not working propery in 4.6
    By deca5423 in forum Qt Programming
    Replies: 4
    Last Post: 28th January 2010, 01:16
  5. Replies: 3
    Last Post: 12th August 2008, 20:14

Tags for this Thread

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.