qt 在 showEvent() 上隐藏控件
我在窗口上调用 show() ,它有几个控件,并且所有控件都会显示。
其中一个控件是继承自QFrame
的自定义控件。
如果设置了特定标志,我想隐藏此控件。所以,我有
void MyCustomControl::showEvent ( QShowEvent * /* evt */ )
{
if (!m_visibleAllowed)
hide();
}
虽然这隐藏了控件,但它使控件变得愚蠢;它看起来冻结了。当调整窗口大小时,控件所在的区域不会刷新。在论坛中搜索,我得到的想法是隐藏控件不应该在 showEvent()
上完成,这是真的吗?如果是这样,那么我应该如何/在哪里尝试隐藏控件。如果可以从 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 resized, 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果问题出在您的 show 事件期间调用 hide() (我无法确认它是否被明确禁止,但这听起来不是一个好主意),并且从您的 show 事件中调用 hide 是您真正需要的地方有了这段代码,您就可以使用单次计时器:
它将简单地将
hide()
函数的执行推迟到下一轮事件循环。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:
which will simply defer the execution of the
hide()
function until the next round of the event loop.也许您可以使用 QStackedLayout 或 QStackedWidget 堆栈中有两个小部件:您的控件和一个“空白”QWidget。如果您这样做,则无需在控件上使用 show() 和 hide(),而是切换堆栈顶部的内容。
这样你就永远不会尝试渲染隐藏的小部件 - 如果你的控件不可见,你会渲染空白的 QWidget - 我怀疑这会解决你的图形故障。
希望这有帮助!
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!