Important: Please read the Qt Code of Conduct -

Resize top level window to fit contents?

  • I'm using a custom dialog as a top level window which, among other things, shows some images in QLabels. If I change the QLabel size I can grow the dialog perfectly fine and everything is still laid out nicely. If I shrink the QLable image size, I'm left with vast amounts of empty space in my dialog. I can grab the window manager handle and resize the window back down to where things will just fit, but I can't for the life of me figure out how the devil to make Qt do that for me. The adjustSize() function is described like it is exactly what I want, but calling it has no effect. How do I make this happen short of sending phony mouse events to emulate a window resize operation?

  • You could try out:


    The point is, the layouts keep track that all widgets fit in the area. But if there is too much space, it's not shrunk.

  • Nah, resize(sizeHint()) has the same zero-effect as everything else I've tried. I may have to destroy the main window and create a new copy from scratch (you'd think there would be a way to ask the top layout to essentially do just that, but if there is, I can't find it).

  • I finally got it to work, what I needed to do was this:

    QTimer::singleShot(0, this, SLOT(resizeMe()));

    where the resizeMe() slot calls:


    The minimum size isn't recomputed till the event loop gets some stuff to run, so by running from the zero length timeout, I can do the resize after the hint is properly updated.

Log in to reply