PDA

View Full Version : Resize main window after hiding an element



Swankee
21st January 2010, 15:53
I have a textarea on my page and a hide and a show button that controls the textarea. When I click 'hide' the textarea hides but I'm left with a blank space area that was previously filled with my textarea. I'd like to have the main window resize when 'hide' is clicked. I found updateGeometry() But the window is resizeable. Is that only available for static values? thanks!

Lykurg
21st January 2010, 16:31
I think you are looking for something like that: http://www.qtcentre.org/wiki.php?title=Expanding_dialog.

The magic word is "setSizeConstraint(QLayout::SetFixedSize)"

B4adle7
22nd February 2012, 23:23
This is one I have just developed an answer for!!! ( Note: this will be given in PyQt4 form )

I have needed the ability to be able to re-size my app window when showing and hiding extra panels.
However in the past I ended up just reconfiguring the UI and/or work flow so that re-sizing the window wasn't necessary. However now it is a needed must.

The window can be re-sized during its creation. The window can be re-sized directly (e.g. self.resize( xSize, ySize )
However if you create some lines of code that capture the needed sizes, hides the desired section, recalculates what the size should be, the re-size seems to be not take at all.

(example)
curHgt = self.size().height()
curWth = self.size().width()
myFrmHgt = self.ui.reviewFrm.size().height()
self.ui.reviewFrm.hide()
self.resize( curWth, (curHgt - myFrmHgt ) )

Nothing. Doesn't work. Why not?

It today's hunt for an answer still did not come up with a viable answer. There are several offerings for options. Hunt up the widget hierarchy, invalidate, adjustSize. A rather exhaustive layout shaking that just will not work if you are attempting to re-size the the main window. There was also an overwhelming response of saying to set your constraint to FixedSize. But that rather defeats the entire purpose, kinda like saying to cut your hand off at the wrist if you have a hang nail.

However I did stumble across someone that had some success with digging in and discovering that the reason the re-size isn't taking hold is because of the queue of Posted Events that will reset the resize.

His solution was to run the QApplication.sendPostedEvents() just before doing your application re-size, so that the queued events don't reset your window.
Yes, and No.
Yes, this is the solution, and I am sure there is a prettier way of doing it, but you need to really shake the the queue out like a washed paint roller first.

Here is my simple snippet that I set up that so far seems to be solid.

result = [ self.size().width(), self.size().height() ]
if self.ui.reviewFrm.isVisible():

result[1] -= self.ui.reviewFrm.size().height()
self.ui.reviewFrm.hide()

while self.size().height() > result[1]:

self.appy.sendPostedEvents()
self.resize( result[0], result[1] )

(Note: self.appy is where I had stored the application object)
Nope. It ain't pretty. However at most I have only seen it shake the paint roller twice before the Posted Events allowed for the window to resize.
No need to lock constraints or shake the parent widgets or climb through the hierarchy looking for size hints.