PDA

View Full Version : recenter child widgets after hide/show



so Qt
30th July 2015, 14:58
Hi,

the QDialogs in my application are often just hidden when some of the buttons is pressed or some other exit-slot is called so that I just can show and don't have to recreate them if they are requested by the user for the 2nd time. Unfortunately this creates some weirdly looking behavior if you accept/kill one of these dialogs at position X, then move the parent window to some other position and show the dialog again. It will then be shown on its last position which is X and not centered on the parent as you'd expect and as it's done if you spawn and therefore create it for the first time.
Is there a simple way to achieve this without having to recreate the dialog everytime that it's shown and hopefully without writing a hacky routine that makes a recentering on each showEvent?

anda_skoa
30th July 2015, 21:13
Sub windows, such as dialogs, are positioned by the windowing system unless they specifically request a position.

Most windowing systems do not reposition such a window when it has just been hidden and then reshown, basically just setting a position on first show.
If you want to reposition on each show, you'll have to implement this yourself

Cheers,
_

so Qt
1st August 2015, 13:06
Yes, I know. I'm just asking what the best practice in a case like this is. The problem is that our widgets are often bloated with functionality which is really bad design but now tough to fix because it has been developed in this way for years and might break a lot of other code if changed. Otherwise I think I'd just kill and recreate the widget the next time it's shown because it's creation should be pretty light-weight then.
Can I just override the showEvent in my subclasses to repositioning the widgets by myself?

anda_skoa
1st August 2015, 13:48
Can I just override the showEvent in my subclasses to repositioning the widgets by myself?
Yes.

Alternatively, if you have many of these sub windows and no common base class, you could also create an event filter based "decorator" that you attach to each window.

Cheers,
_