16

I have a QGLWidget, which I want to resize to a given resolution (bigger than the containing window). My intention is, that the window expands until the widget fits inside, but can't find a way to do it.

I tried several commands after resizing the QGLWidget to make it work without success. I will list the results here:

  • do nothing else: The Widget overlaps the whole window. Eventually it will be resized to fit back into the smaller window
  • mainWindow.adjustSize(): The widget gets resized to (0, 0)
  • mainWindow.resize(mainWindow.sizeHint()): see above
  • mainWindow.resize(mainWindow.minimumSizeHint()): see above

I also read in this thread, that before doing the mainWindow resize I the event loop needs to be run to recalculate the new sizes, so I inserted QCoreApplication::processEvents to do so, without any visible effect.

So how do I resize the window via the widget?

Edit

The GLWidget is not the only widget of the window. It is embedded in splitter together with a group box.

  • Hi Sorry, I misclicked the downvote button but I didn't mean it at all. I tried to undo, but it doesn't let me do it. Please apologize me :( – ismailsunni Jul 07 '20 at 07:28
  • So... have you found a way around this? I have exactly the same problem, and I have to compromised with `setMinimumSize()`. –  Jun 03 '21 at 00:00

2 Answers2

13

http://qt-project.org/doc/qt-4.8/qwidget.html#sizePolicy-prop

http://qt-project.org/doc/qt-4.8/qsizepolicy.html#Policy-enum

http://qt-project.org/doc/qt-4.8/qwidget.html#setFixedSize

So assuming that you have your QGLWidget nested inside your QMainWindow as the central widget, you need to set the size policy of your QGLWidget.

For example:

QGLWidget * glw; // in your header for QMainWindow

...

// In your constructor for QMainWindow
glw = new QGLWidget;
this->setCentralWidget(glw);
glw->setFixedSize(500, 500);

this->adjustSize();

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
  • While this really resizes the window to the needed size it prevents from further resizing by the user. From the documentation: `Sets both the minimum and maximum sizes of the widget to s, thereby preventing it from ever growing or shrinking.` I just tried to save and restore the old minimum and maximum sizes, but this results in no change at all. – Nobody moving away from SE May 10 '13 at 10:04
  • Instead of `setFixedSize()`, try `setMinimumSize()`, and use `setSizePolicy()`. You really have a lot of control over how things expand and react using `setSizePolicy()`. `setFixedSize()` does a bunch of things at once, so it probably isn't what you want in this case. – phyatt May 10 '13 at 16:55
  • `minimumSize` seems also the wrong thing to me. The user should be able to resize arbitrarily. I just need the window to pop to the size that fits the requested size of the `QGLWidget`. – Nobody moving away from SE May 10 '13 at 17:02
  • Have you tried `glw->setSizeHint(500,500);` followed by `this->adjustSize()`, or `this->resize(glw->sizeHint());`? – phyatt May 10 '13 at 17:10
  • 1
    Hm there seems to be no `setSizeHint()` function. And won't `this->resize(glw->sizeHint()` make the window a too small (because of the borders and other widgets)? – Nobody moving away from SE May 10 '13 at 18:07
  • 1
    You are right. Here is the thorough explanation on it: http://qt-project.org/doc/qt-4.8/application-windows.html – phyatt May 10 '13 at 20:38
  • setFixedSize is almost never a good idea... it takes out the whole flexibility – IceFire Dec 23 '15 at 13:53
4

I have an app that needed to be very similar to your requirements, so I'll post my solution here. An image covering the window which is freely expandable and shrinkable, and can be changed to the original size, and remain expandable / shrinkable after that.

I used a QLabel widget to display the image, but it should work with other widget types too. I created the widget with an initial size and the QSizePolicy::Ignored.

label->resize (w, h); // initial size
label->setSizePolicy (QSizePolicy::Ignored, QSizePolicy::Ignored);

The label widget was in a QVBoxLayout with a few buttons in the window, but this may work with other layout types too.

The window and image widget can be resized to the image's original size with this code:

label->resize (w, h); // change to original size
label->setMinimumSize (w, h); // prevent it from collapsing to zero immediately
window->adjustSize (); // resize the window
label->setMinimumSize (0, 0); // allow shrinking afterwards
gregn3
  • 1,728
  • 2
  • 19
  • 27
  • Unfortunately, this does not work for me. The widget is resized but the `adjustSize()` on the window collapses the window below the size of the widget. Maybe it is caused by the splitters that I am using. – Nobody moving away from SE Nov 26 '14 at 08:14