Getting maximum size a widget can be in a layout
-
Is there a way to get the size a widget in a layout can be if it expanded to fill all possible space? For example, if two widgets shared a layout and they both have an expanding policy (i.e., share the layout space equally), is it possible to get the maximum size the first widget could be if it were to take space from the second widget?
QWidget has a method for getting the maximum size, but this value is in absolute terms and not relevant to the maximum size it could take in a layout.
-
@ChrisW67 said in Getting maximum size a widget can be in a layout:
Out of interest, why do you want to know this?
I have a QTextEdit inside of custom scroll area, which is inside a container widget, where that container widget should resize to fit the content of the QTextEdit. If it can't, then the text in the QTextEdit will wrap. The issue is that the QTextEdit has a fixed width equal to the container to ensure that the text in it wraps if it would overflow the container width. This causes newly input text to initially be shown on a new line, then when the QTextEdit adjusts its size hint and that size is reflected in the container, the QTextEdit may be resized again which causes the text input on the new line to be moved back to the original line it was input on. This happens quickly, but it's still visible when typing quickly.
My thoughts were that I could have a hidden 'buffer' QTextEdit that has the same styling as the displayed QTextEdit, so when text is input the buffer QTextEdit intercepts and inserts that text into itself so that it can determine what the unbounded document size is. Based on the container's max size in the layout, if the container can resize to fit the text, then the displayed QTextEdit will be resized to that size and the buffer QTextEdit will insert its text into it. Otherwise, the buffer QTextEdit will adjust its own size to the container's max size, which will update the text document size, then this new size will be applied to the displayed QTextEdit before the text is finally inserted. In short, the displayed QTextEdit is only ever updated after the buffer QTextEdit has a chance to determine what size it would be, which will hopefully fix the visual 'jumping' that resizing the container and displayed QTextEdit causes without a buffer QTextEdit. It may also be possible to just use a QTextDocument and style the blocks in it instead of using an entire QTextEdit.
After thinking it over, I might not need to know the max size if I set the size hint of the container and try to resize it. After it's resized, I can use that size instead of needing to know the max size it can be in the layout.
-
If these are the only widgets in the layout then I suppose you could ask the containing layout (i.e. the parent widget's layout()) for its QLayout::contentsRect() or geometry().
More crudely, you could hide() the second widget and see what happens to the first.
Out of interest, why do you want to know this?
-
@ChrisW67 said in Getting maximum size a widget can be in a layout:
Out of interest, why do you want to know this?
I have a QTextEdit inside of custom scroll area, which is inside a container widget, where that container widget should resize to fit the content of the QTextEdit. If it can't, then the text in the QTextEdit will wrap. The issue is that the QTextEdit has a fixed width equal to the container to ensure that the text in it wraps if it would overflow the container width. This causes newly input text to initially be shown on a new line, then when the QTextEdit adjusts its size hint and that size is reflected in the container, the QTextEdit may be resized again which causes the text input on the new line to be moved back to the original line it was input on. This happens quickly, but it's still visible when typing quickly.
My thoughts were that I could have a hidden 'buffer' QTextEdit that has the same styling as the displayed QTextEdit, so when text is input the buffer QTextEdit intercepts and inserts that text into itself so that it can determine what the unbounded document size is. Based on the container's max size in the layout, if the container can resize to fit the text, then the displayed QTextEdit will be resized to that size and the buffer QTextEdit will insert its text into it. Otherwise, the buffer QTextEdit will adjust its own size to the container's max size, which will update the text document size, then this new size will be applied to the displayed QTextEdit before the text is finally inserted. In short, the displayed QTextEdit is only ever updated after the buffer QTextEdit has a chance to determine what size it would be, which will hopefully fix the visual 'jumping' that resizing the container and displayed QTextEdit causes without a buffer QTextEdit. It may also be possible to just use a QTextDocument and style the blocks in it instead of using an entire QTextEdit.
After thinking it over, I might not need to know the max size if I set the size hint of the container and try to resize it. After it's resized, I can use that size instead of needing to know the max size it can be in the layout.