[Interest] Automatically resize widget based on a child table's size

Murphy, Sean smurphy at walbro.com
Fri Jul 19 21:29:44 CEST 2019

I'm trying to create a widget with the following setup (this is related to my popup window question last week):

Widget hierarchy
popupWindow - unparented, top level window
 - QVBoxLayout
   - genericWidget (in my case it's a QGroupBox, but it could be any type of widget in theory)
   - QTableView

The QTableView is configured with both resizeRowsToContents & resizeColumnsToContents. My goal is that I want the popupWindow to automatically resize itself to be JUST BIG ENOUGH to show the QGroupBox and the entire QTableView without scrollbars if possible, up to a certain maximum popupWindow size, at which point the popupWindow doesn't get any larger and the table has to show scrollbars. 

I set the popupWindow's maximum size based on the user's desktop/application geometry to limit the popupWindow to something that looks reasonably like a popup - not something that would take up the full screen. But from one popup event to the next one, the underlying table data may have completely changed, which in turn causes the minimum table size required to keep the scrollbars hidden to change from one showing to the next. The issue I'm having is because the table is HIDDEN when the data is swapped out any queries I'm doing on the table's size don't seem to be accurate before I actually show it, but I want the popupWindow to show only once and already be sized correctly without any flicker. 

So the basic algorithm is always:
1. popupWindow has a maximum allowed size, sizeMax
2. popupWindow (and children) are hidden
3. table data changes
4. compute ideal table size to show all data without any scrollbars, sizeIdeal
5. calculate actualSize by comparing ideal size to max size, and picking minimum of each dimension:
    actualSize.setWidth(sizeIdeal.width() < sizeMax.width() ? sizeIdeal.width() : sizeMax.width())
    actualSize.setHeight(sizeIdeal.height() + groupBox.height() < sizeMax.height() ? sizeIdeal.height() + groupBox.height() : sizeMax.height())
6. Set popupWindow's fixed size to actualSize
7. Show popupWindow at correct size

It's step 4 that I'm having trouble with... I feel like I'm just not combining size policies correctly to get the behavior I'm looking for

P.S. Here's an example with numbers: say the groupBox's minimize size is 100 x 50 and the maximum allowed size for the popupWindow is 600 x 800. Then the following sequence of popups occurs:

First popup
 - the data set populating the table needs 300 x 250 to show all contents without scrollbars
 - popupWindow should resize itself to be 300 x 300 (required table width x (minimum table height + groupBox height))

Second popup
 - the data set populating the table would need 1000 x 1200 pixels to show contents without scrollbars
- popupWindow should resize itself to be 600 x 800 (maximum allowed popup size) which means the table is clipped in both dimensions and therefore shows scrollbars in both dimensions

Third popup
 - the data set populating the table needs 400 x 780 pixels to show contents without scrollbars
 - because of the groupBox minimum height of 50, our ideal popupWindow height would be 830, but that exceeds the 800 pixel limit 
 - popupWindow should resize itself to be 400 x 800 (minimum table width x maximum allowed popup height) and the table shows a vertical scrollbar to accommodate the clipped height. Actually I guess the popupWindow would resize itself to something like 420 x 800 to allow 400 pixels for the table data and 20 pixels for the vertical scrollbar's width.

Fourth popup, we return to the same data set as the first popup
 - the table only needs 300 x 250 to show contents without scrollbars
 - popupWindow should resize itself to again be 300 x 300 

This message has been scanned for malware by Forcepoint. www.forcepoint.com

More information about the Interest mailing list