I have less than 1 day of experience in QT (that's why I do not know much of it) I have a window full of information (labels, text, buttons, etc) organized by layouts.
I need that after I press one button, all of the components in a window be hidden (which I already did) except for one label which should increase to barely the size of the whole window
Despite I tried modifying the "geometry" attribute (with code) the hidden layouts do not let the label to be increased. I thought also of using the option of layout breaking, but the label losses its dynamism. Could anyone please recommend me anything to do? Thanks.
Has anyone done something like this before. Thanks.
I once provided an answer to SO: Qt - How to create Image that scale with window, and keeps aspect ratio?. The actual intention was to scale an image in a
QLabel
with original aspect ratio to consume maximum available size.However, I got the feedback that the suggested solution would not work properly when my Label would be used in a
QGridLayout
. (This sounds very similar to the issue of the OP.) Hence, I modified the sample to reproduce the issue and fiddled a little bit around with. For me, it seems that resize events of the main window are processed in theQGridLayout
but affect layouted image label only partially. (Shrinking is applied but growing not.) Fortunately, I found a very simple work-around: Setting a non-empty frame to theQLabel
solved the problem. I had a look into the source code on woboq.org. I hoped to get a hint what the changed frame style would activate (to apply this as fix for my resize issue). Finally, I was not patient enough and put it aside.Beside of this
QLabel
in aQGridLayout
resize issue, changing the visibility of widgets should cause a proper re-layout. I would prefer show/hide (instead of delete and re-new) as this is surely easier to implement, more efficient, and less error-prone.I took the old sample code and added a tool button which can be used to toggle the visibilty of some of the layouted widgets:
I compiled and tested in VS2013 on Windows 10:
After toggling the Decoration tool button:
Note:
Out of curiosity, I commented the line which changes the frame style
and again, resizing of image didn't work properly anymore.
You can remove and hide widgets inside a layout using
QLayout::removeWidget(*widget)
; but you do not need to actually remove it. You should useQWidget::hide()
for the content to disappear and for the video label's cell to be able to take that space. I think you need to pay attention to the video label's size policy if it does not increase in size. Assuming you have aQGridLayout
like so:And let's say, when you click
button3
,label1
,label2
andlabel4
should all disappear andvideoLabel
takes the newly created space. I would group the widgetslabel1
,label2
,label4
andvideoLabel
into a single widget having its own sub-layout. I useQSizePolicy::Expaning
to make sure myvideoLabel
takes the maximum space possible. Here is the implementation:I got the following results for this:
Keep in mind there are other approaches to this question. This one need not be necessarily the best when it comes to memory, but it is quite easy to follow.