I have a QGraphicsView
that renders my game. This view fills the entire screen at all times. In the top right I have another QGraphicsView
which I'm using as a mini-map; it sits over the game view. I want this mini-map to be anchored to the top right of the screen, always maintaining its size. This code almost works, except that the left side of the mini-map never changes (which is to be expected).
void MainWindow::resizeEvent(QResizeEvent *event)
{
mainWindow->graphicsView->resize(event->size().width(), event->size().height());
QRect newRect(mainWindow->miniMapGraphicsView->geometry());
newRect.setRight(event->size().width() - 20);
mainWindow->miniMapGraphicsView->setGeometry(newRect);
}
How can I do this?
When you want to position widgets in a certain way, layouts are usually the best way to do it.
Add a QFormLayout
to your graphicsView
and set its layoutDirection
to Qt::RightToLeft
. Then add your miniMapGraphicsView
to the layout. Editing the properties of the mini map, set its horizontal and vertical sizePolicy
to Fixed
and set its minimumSize
and maximumSize
to the dimensions you would like it to be.
An alternative would be to use a QGridLayout
and use horizontal and vertical spacers to push the mini map to any corner of the view.
NOTE: Layouts have margins set by default so if you want your widgets to align snugly at the edges, zero them out.