Results 1 to 4 of 4

Thread: Resizing a Qt main window when a contained widget changes its size

  1. #1
    Join Date
    Dec 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Resizing a Qt main window when a contained widget changes its size

    In Qt4, I have a main window which sets as its central (and only) widget a custom widget I created. Through user action, the widget can change its size. When this happens, I want the main window that contains the widget to adjust its size so that it conforms to the new size of its child widget. Unfortunately, this isn't happening. The new widget is created but it is forced to adapt to the main window's previous size. I have over-ridden the sizeHint method for the widget so that it returns the desired size but this appears to be ignored. I've also added a call to updateGeometry for both the widget and the main window but this has no affect either.

    I'm pretty sure I'm close to getting this to work but I'm not terribly experienced in Qt's nuances. Can someone give me some guidance here?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Resizing a Qt main window when a contained widget changes its size

    I'd say all you need is to set a layout in your parent widget, and embbed the custom widget in to that layout - the layout will take care of size adjustments - no code on your side will be necessary.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Dec 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Resizing a Qt main window when a contained widget changes its size

    I did try that but when I call addLayout on the main window, I get an error message about attempting to set a layout on the main window which already has a layout. I also tried adding the layout I created as the central widget but that doesn't work either.

    I assume that QMainWindow creates a layout automatically when it created. Is there a way to remove this layout and install my own?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Resizing a Qt main window when a contained widget changes its size

    Why are you using a QMainWidow?
    QMainWindow by design has an "inner space" which can be larger than the screen, therefore its size is not supposed to be adjusted to the inner content, and if you need that, probably QMainWindow is not what you need.
    A QDialog probably would suit you better.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 10
    Last Post: 16th July 2011, 12:29
  2. Main Window minimal size constraints
    By psih128 in forum Qt Programming
    Replies: 1
    Last Post: 3rd August 2010, 17:06
  3. Replies: 3
    Last Post: 2nd March 2010, 20:58
  4. Main Window size problem
    By frenk_castle in forum Newbie
    Replies: 2
    Last Post: 20th December 2009, 10:49
  5. resizing widgets depending on a main widget size
    By luf in forum Qt Programming
    Replies: 6
    Last Post: 10th October 2009, 16:13

Tags for this Thread

Bookmarks

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.