Results 1 to 6 of 6

Thread: Creating a square sized widget in a layout

  1. #1
    Join Date
    Mar 2006
    Posts
    46
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Creating a square sized widget in a layout

    I have a QGridLayout something like this:

    Qt Code:
    1. QGridLayout gLayout;
    2. gLayout->addWidget(new CustomWidget1,0,0,1,1);
    3. gLayout->addWidget(new CustomWidget2,0,1,1,1);
    To copy to clipboard, switch view to plain text mode 

    As you can see above, the two widgets are laid side by side in a single grid cell each, e.g. :

    CW1 | CW2

    CustomWidget1 has a size determined by the data it is displaying, and it determines the overall height of gLayout.

    CustomWidget2 is an optional display that can be shown or hidden. Showing it should expand the dialog width, but not change the height. I would like CW2 to have a width that matches it's height (which is in turn determined by CW1 and gLayout), making it a square widget. I can't seem to figure out how to do this cleanly without CW2 knowing what size CW1 is through poorly encapsulated means.

    Put another way:

    What is the best way to create a widget with a flexible height that always has an actual width that matches its actual height?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Creating a square sized widget in a layout

    In your case it should be the best to reimplement resizeEvent and trigger another resize there to make sure the widget is square.

    If you wanted to match the height to width of the widget (so the other way round), you could use QWidget::heightForWidth.

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

    cboles (24th April 2006)

  4. #3
    Join Date
    Mar 2006
    Posts
    46
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a square sized widget in a layout

    Thanks - it would be nice if there was a widthForHeight() as well for completeness in the QWidget class.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Creating a square sized widget in a layout

    I'm sure you can subclass QWidget and implement widthForHeight capabilities.

  6. #5
    Join Date
    Mar 2006
    Posts
    46
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a square sized widget in a layout

    True, but none of the Qt layout related classes will know about this new method. I thought that the layout classes call heightForWidth() to help calculate the layout? I may be able to find a way to trick these classes into doing what I want, but it seems like that would rely on knowing their implementation, which could change in the future.

    Quote Originally Posted by wysota
    I'm sure you can subclass QWidget and implement widthForHeight capabilities.

  7. #6
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a square sized widget in a layout

    I also have a need for widthForHeight() method. A container, QScrollArea, uses a custom flow layout that lays out table views in a single row. The container has horizontal scroll bar but no vertical scroll bar. The table views, on the opposite, do not have horizontal scroll bars and only have vertical scroll bars. The problem is when the container's hight gets small enough so that it cuts-off bottom parts of the tables, the tables need to display vertical scroll bars. When this happens, minimalSizeHint() and sizeHint() need to adjust for the vertical scroll bar's width. But in the Qt layout system there seems to be no way to tell a child widget to compute its width based on the vertical space that the parent can provide for the child. Any advice on how to solve this will be greatly appreciated.

    Thank you,
    Yuri

Similar Threads

  1. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 08:06
  2. changing layout of a widget
    By mikro in forum Qt Programming
    Replies: 10
    Last Post: 4th August 2009, 21:21
  3. Resize widget force layout resizing
    By ^NyAw^ in forum Qt Programming
    Replies: 17
    Last Post: 11th February 2009, 12:27
  4. Replies: 1
    Last Post: 18th March 2006, 11:18

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.