Results 1 to 3 of 3

Thread: Trying to use Layouts and QGraphicsView to resize widget for any monitor resolution

  1. #1
    Join Date
    Oct 2015
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Trying to use Layouts and QGraphicsView to resize widget for any monitor resolution

    I am trying to design a window that can be displayed fullscreen on monitors of various aspect ratios and resolutions. The window is being made in Designer and the central widget is a Horizontal Layout. The 3 items in the horizontal layout are a GroupBox on the left, a GraphicsView in the center, and another GroupBox on the right. The GroupBoxes may consist of text and/or buttons. The GraphicsView should display a view of a scene that is merely a circle drawn using QGraphicsScene::addEllipse(). The GraphicsView should be maximum in height and only as wide as its height, because its contents will always be a circle (displayed from edge to edge). The window should be displayed fullscreen and resize so the GraphicsView is as tall as possible and square (maintaining aspect ratio).

    I've had trouble doing this for the last day or so... I can get variations of it to work, but I either can't get the GraphicsView to display the circle properly using QGraphicsView::fitInView(), or I can't keep the GraphicsView from becoming too wide (wider than its height). I've attached a simple sample application and some screenshots below. Any help is appreciated!

    Cheers,
    Ryan

    p.s. I'm have tested on Qt 4.8.1 and Visual Studio 2010 and Qt 5.8.0 and MSVC2015.


    How it looks in designerDesignerCapture.jpg


    Results fullscreen on 1920x1080RuntimeCapture.jpg


    Desired results: GraphicsView in Red, GroupBoxes in Blue, Circle in Green: DesiredResults.jpg
    Attached Files Attached Files

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Trying to use Layouts and QGraphicsView to resize widget for any monitor resoluti

    Reset all the size restrictions you have put in the designer to defaults on all the widgets and layouts and just implement resizeEvent of MainWindow, like this

    Qt Code:
    1. void MainWindow::resizeEvent(QResizeEvent * event)
    2. {
    3. QMainWindow::resizeEvent(event);
    4.  
    5. ui->graphicsView->setFixedWidth(event->size().height());
    6.  
    7. if(ui->graphicsView->scene())
    8. ui->graphicsView->fitInView(ui->graphicsView->scene()->sceneRect(), Qt::KeepAspectRatio);
    9. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    rradjabi (12th May 2017)

  4. #3
    Join Date
    Oct 2015
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trying to use Layouts and QGraphicsView to resize widget for any monitor resoluti

    Thank you Santosh! This was the perfect solution and it behaves exactly as I wish now.

Similar Threads

  1. Replies: 1
    Last Post: 5th September 2013, 16:33
  2. QGraphicsview with bad resolution
    By raphael_ in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2012, 16:47
  3. Replies: 3
    Last Post: 11th December 2011, 10:09
  4. Replies: 8
    Last Post: 20th June 2010, 19:15
  5. How to calculate Monitor Resolution?
    By ashukla in forum Qt Programming
    Replies: 2
    Last Post: 7th October 2007, 07:28

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.