qt 在 showEvent() 上隐藏控件

发布于 2024-12-07 04:22:15 字数 456 浏览 1 评论 0原文

我在窗口上调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守望孤独 2024-12-14 04:22:16

如果问题出在您的 show 事件期间调用 hide() (我无法确认它是否被明确禁止,但这听起来不是一个好主意),并且从您的 show 事件中调用 hide 是您真正需要的地方有了这段代码,您就可以使用单次计时器:

QTimer::singleShot( 0, this, SLOT(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:

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

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

独守阴晴ぅ圆缺 2024-12-14 04:22:16

也许您可以使用 QStackedLayoutQStackedWidget 堆栈中有两个小部件:您的控件和一个“空白”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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文