Results 1 to 4 of 4

Thread: Set minimum size of a container without distorting its children

  1. #1
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Set minimum size of a container without distorting its children

    Hi,

    I have a QWidget (QFrame) container with a few widgets inside (usually QTableView, Qwt plot, etc...). The contents of this container may change dynamically.

    Generally, the container's minimum size is properly auto-calculated according to its contents using the layouts system, which helps avoiding distortion of its child widgets.

    However, I want this container's size to be at least 200x200. That is, the container's minimum size should be:
    max(200x200, contents_size).

    This is where it becomes problematic. If I use
    Qt Code:
    1. container->setMinimumSize(200, 200)
    To copy to clipboard, switch view to plain text mode 
    and the total size of the contents is greater than 200x200, this minimumSize allows the container to be resized to 200x200 which may distort or chop off its children.

    I'm attaching an example minsize.ui which shows the problem - QFrame with minimumSize of 100x100 allows the child buttons to get distorted. In reality, I want the minimum size to be "at least 100x100, but more if needed".

    Is there a way to fix this? I'm using Qt 5.9.

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set minimum size of a container without distorting its children

    You can try this:

    1) Create a subclass of QFrame
    2) overwrite minimumSizeHint() such that it takes the value from the base class and expands it.
    Something like
    Qt Code:
    1. QSize MyFrame::minimumSizeHint() const
    2. {
    3. const QSize baseSize = QFrame::minimumSizeHint();
    4.  
    5. return baseSize.expandedTo(QSize(200, 200));
    6. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    alex_sh (11th February 2019)

  4. #3
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Set minimum size of a container without distorting its children

    Quote Originally Posted by anda_skoa View Post
    You can try this:

    1) Create a subclass of QFrame
    2) overwrite minimumSizeHint() such that it takes the value from the base class and expands it.
    _

    Thanks, that would work I think.

    I kinda wanted to do it in Qt Designer without reverting to subclassing and stuff, but I guess there's no other way...

    It's quite strange that Qt doesn't provide this facility out of the box though.

    Thanks!
    Alex

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set minimum size of a container without distorting its children

    Quote Originally Posted by alex_sh View Post
    I kinda wanted to do it in Qt Designer
    You can still use Designer:
    1) Add a frame
    2) Use the "promote widget" feature to replace it with your frame subclass when the code gets generated by UIC.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    alex_sh (13th February 2019)

Similar Threads

  1. Replies: 6
    Last Post: 26th December 2015, 18:33
  2. Qt static link with minimum size
    By foens in forum Installation and Deployment
    Replies: 0
    Last Post: 25th February 2010, 13:09
  3. Replies: 2
    Last Post: 23rd March 2009, 18:26
  4. Minimum QGroupBox size
    By klnusbaum in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2008, 18:22
  5. QMenuBar minimum size
    By Angelo Moriconi in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2007, 22:14

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.