PDA

View Full Version : Trying to use Layouts and QGraphicsView to resize widget for any monitor resolution



rradjabi
11th May 2017, 19:52
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 designer12459


Results fullscreen on 1920x108012460


Desired results: GraphicsView in Red, GroupBoxes in Blue, Circle in Green: 12461

Santosh Reddy
12th May 2017, 14:27
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



void MainWindow::resizeEvent(QResizeEvent * event)
{
QMainWindow::resizeEvent(event);

ui->graphicsView->setFixedWidth(event->size().height());

if(ui->graphicsView->scene())
ui->graphicsView->fitInView(ui->graphicsView->scene()->sceneRect(), Qt::KeepAspectRatio);
}

rradjabi
12th May 2017, 20:43
Thank you Santosh! This was the perfect solution and it behaves exactly as I wish now.