Results 1 to 1 of 1

Thread: (not a question) : how to make a main window resize to its contents

  1. #1
    Join Date
    Jun 2012
    Paris, France
    Thanked 2 Times in 2 Posts
    Qt products

    Lightbulb (not a question) : how to make a main window resize to its contents

    I spent quite a lot of time finding an answer to this question, so I decided to share the findings.
    I was struck by the blinding flash of the obvious reading this thread, but it seems Google has not deemed it as interresting as I do.
    Besides, instead of providing a lenghty example, I decided to narrow it down to the two essential lines of code.
    So here it goes:

    Sometimes you may want your main window to resize to its current (dynamic) contents.
    In my case I wanted my silly brickout game to resize according to its level layout :

    The trick is simply to apply a fixed-size layout to the main window.

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. , ui(new Ui::MainWindow)
    4. {
    5. // trick to get the main window to resize to its contents
    6. layout()->setSizeConstraint(QLayout::SetFixedSize);
    8. // rest of constructor code...
    9. }
    To copy to clipboard, switch view to plain text mode 

    In this example I use a MainWindow straight from QT designer, but the same can be done with any hand-coded class.

    Hope some people will find that useful...
    Last edited by kuroi_neko; 8th June 2012 at 15:22.

Similar Threads

  1. Resize main window after hiding an element
    By Swankee in forum Newbie
    Replies: 2
    Last Post: 23rd February 2012, 00:23
  2. Maximize window and resize contents - howto?
    By hakermania in forum Newbie
    Replies: 4
    Last Post: 28th June 2011, 15:16
  3. Resize of main window without setFixedSize
    By ElyBeta in forum Qt Programming
    Replies: 2
    Last Post: 15th July 2010, 16:09
  4. main window resize question
    By schnitzel in forum Qt Programming
    Replies: 12
    Last Post: 29th May 2010, 10:31
  5. Replies: 11
    Last Post: 11th August 2008, 10:14

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.