Note that there are some explanatory texts on larger screens.

plurals
  1. POQt fitInView and resizing
    text
    copied!<p>I am trying to make a <code>QGraphicsView</code> with same width as height positioned in center of the window.</p> <p>I created a ordinary <code>QGraphicsView</code> in Qt Designer and set minimal size, added some calculations that center the <code>QGraphicsView</code> into center of the main window and that set the same width as height. It uses only <code>setGeometry</code>. Than I created a <code>QGraphicsScene</code> with only one very long rectangle. I want the graphics scene to fit to view so I have used <code>QGraphicsView::fitInView</code>. Everything works OK, but the problem cames with resizing window.</p> <p>When I increase height and width of the window everything also works fine. The <code>QGraphicsView</code> has a new position and a new size. When I reduce only a part of size of the window everything still works fine. But (finally) when I reduce the size of window to the possible minimum everything breaks. The rectangle in view has the right width (the same as without resizing, I have measured it with a ruler :) ), but a new margin is created and also <code>QGraphicsView</code> is not positioned and sized right (so it is not only the margin, but maybe also the <code>setGeometry</code> has no effect).</p> <p>I have noticed, that the same thing happens when I call <code>QGraphicsView::fitInView</code> before using <code>setGeometry</code>.</p> <p>It is getting me crazy, please help!</p> <p>Here is some code:</p> <pre><code>void MainWindow::resizeEvent(QResizeEvent *e) { int h = e-&gt;size().height(), w = e-&gt;size().width(), s; if(w &gt; h) s = h-120; else s = w-120; ui-&gt;board-&gt;setGeometry((w-s)/2,(h-s)/2,s,s); int scaleWidth = ui-&gt;board-&gt;scene()-&gt;width(), scaleHeight = ui-&gt;board-&gt;scene()-&gt;height(); ui-&gt;board-&gt;fitInView(QRectF(0, 0, scaleWidth, scaleHeight), Qt::KeepAspectRatio); } </code></pre> <p>Here are images of what happens to <code>QGraphicsView</code>, the rectangle is red, view is blue:</p> <p><img src="https://i.stack.imgur.com/8Sj5l.png" alt="this is OK"> <img src="https://i.stack.imgur.com/pqdPf.png" alt="this is broken"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload