开发者

qt hiding a control on showEvent()

I call show() on a window and it has several controls and all controls are displayed.

One of the controls is a custom control that inherits from QFrame.

I want to hide this control if a particular flag is set. So, I have

void MyCustomControl::showEvent ( QShowEvent * /* evt */ )
{
    if (!m_visibleAllowed)
        hide();
} 

While this hides the control, it makes the control goofy; it looks frozen. When the window is r开发者_StackOverflowesized, the area where the control is supposed to be does not get refreshed. Searching around forums, the idea that I get is that hiding the control is not supposed to be done on showEvent() is that true? if so then how/where should I try to hide the control. If hiding the control from showEvent() is possible, how can I prevent the control getting frozen.

Thanks for you time.


If the problem is with calling hide() during your show event (I can't confirm that it's explicitly disallowed, but it doesn't sound like a good idea in general) and calling hide from your show event is where you really need to have this code then you could use a single shot timer:

QTimer::singleShot( 0, this, SLOT(hide()) );

which will simply defer the execution of the hide() function until the next round of the event loop.


Maybe you could use a QStackedLayout or a QStackedWidget that has two widgets in the stack: your control, and a "blank" QWidget. If you did that, instead of using show() and hide() on your control, you switch what's on top of the stack.

That way you never try to render a hidden widget - if your control isn't visible, you render the blank QWidget instead - and I suspect this will solve your graphics glitches.

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜