Results 1 to 5 of 5

Thread: Centering a Widget in Graphics View

  1. #1
    Join Date
    Jun 2009
    Posts
    5

    Default Centering a Widget in Graphics View

    I've been trying for some time now to squash a bug that I can't seem to figure out. I'm using a QGraphicsView as a parent window for my child dialogs. This way I can have a animated background behind my half transparent dialogs.

    The issue is that these dialogs keep appearing in random locations in the QGraphicsView. They start more or less in the center of the view, but as I create and destroy them, they seem to be created in random locations in the view. Here's the code I use to create the dialogs, this is in my subclassed QGraphicsScene:

    Qt Code:
    1. ab = getCurrentPanel(poffset); // Get the next dialog QWidget based on the current id
    2. ab->load(); // perform some initial data population
    3. cproxy = addWidget(ab, Qt::Widget );
    4. QTransform transform;
    5. transform.translate(0, 0);
    6. cproxy->setTransform(transform);
    7. parentView->centerOn(cproxy);
    To copy to clipboard, switch view to plain text mode 

    All the getCurrentPanel() does is return a QWidget pointer to my dialog based on the dialog Id being sent. The constructor for this dialog just passes NULL to the QWidget constructor, so that's not the issue.

    Any ideas?

    Thanks

  2. #2
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Centering a Widget in Graphics View

    QGraphicsView::centerOn just adjusts the scroll bars of the view. The API doc says:

    Note: If the item is close to or outside the border, it will be visible in the view, but not centered.
    If you don't translate / scroll your view, the origin of the scene will be in the centre of your view. So maybe this will work for you:

    Qt Code:
    1. QTransform transform;
    2. transform.translate(-cproxy->width() / 2.0, -cproxy->height() / 2.0);
    3. cproxy->setTransform(transform);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2009
    Posts
    5

    Default Re: Centering a Widget in Graphics View

    Unfortunately, no that doesn't work. Firstly, it seems that QGraphicsProxies don't have a "width" or "height" property, I changed it to:

    Qt Code:
    1. transform.translate(-cproxy->size().width() / 2.0, -cproxy->size().height() / 2.0);
    To copy to clipboard, switch view to plain text mode 

    and it compiles and runs, but I get the same issues.

    Any other ideas?

  4. #4
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Centering a Widget in Graphics View

    Had som similar issues, and I ended up drawing a transparent QRect that was the way above the size of the GraphicsView, and made sure the widgets were placed within this one. That way centerOn worked like a charm.

    I do think this is by design, and not a bug...

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Centering a Widget in Graphics View

    You could try QGraphicsView::fitInView also. Not sure if it will help, but I guess so

Similar Threads

  1. Trivial graphics view framework coordination problem
    By Miihkali in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2009, 08:51
  2. Graphics View Panning ,zooming
    By linuxdev in forum Qt Programming
    Replies: 3
    Last Post: 29th December 2008, 07:17
  3. Replies: 4
    Last Post: 5th August 2008, 19:55
  4. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 07:08
  5. Adding Rectangular overlay on graphics view
    By forrestfsu in forum Qt Programming
    Replies: 10
    Last Post: 21st November 2006, 19:42

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.