简体   繁体   English

Qt创建者:Qwidget的paintEvent

[英]Qt creator: paintEvent of Qwidget

I have some problems. 我有一些问题。 Hope anyone can help me. 希望任何人都能帮助我。 I have a Qwidget1 and Qwidget2. 我有一个Qwidget1和Qwidget2。 Qwidget1 have a widget that promote to Qwidget2. Qwidget1有一个提升为Qwidget2的小部件。 Both Qwidget1 and Qwidget2 have paintEvent. Qwidget1和Qwidget2都具有paintEvent。 I have writed "qDebug()<< "Update"; " in paint event of Qwidget1. 我在Qwidget1的绘画事件中写了“ qDebug()<<” Update“;”。 When I run project, I see a word "Update" has been printed a lot of times. 当我运行项目时,我看到“更新”一词已经打印了很多次。 So why Qwidget1 execute paint event a lot of times. 那么,为什么Qwidget1会多次执行绘画事件。 How can I fix it, just execute paint event when show Qwidget1 at the first time and when I call update. 我如何解决它,仅在第一次显示Qwidget1时以及在我调用update时执行paint事件。

This is expected behavior. 这是预期的行为。 Your code works like it should. 您的代码按应有的方式工作。 From Qt documentation: 从Qt文档中:

A paint event is a request to repaint all or part of a widget. 绘制事件是重新绘制小部件的全部或一部分的请求。 It can happen for one of the following reasons: 可能由于以下原因之一而发生:

repaint() or update() was invoked, 调用了repaint()或update(),

the widget was obscured and has now been uncovered, 该小部件已被遮盖,现已被发现,

or many other reasons. 或许多其他原因。

There can be any number of situations when a window or its part becomes invalidated and has to be repainted. 窗口或其部分失效并需要重新粉刷的情况可能有很多。 Such situations include, but are not limited to: 此类情况包括但不限于:

  • window size change (including minimizing / maximizing / restoring the window); 窗口大小更改(包括最小化/最大化/还原窗口);
  • mouse pointer passing over a widget - it may or may not trigger repaint; 鼠标指针越过窗口小部件-它可能触发也可能不会触发重新绘制;
  • other window moving over the window in question. 另一个窗口移到相关窗口上。

When it happens, Windows will send the WM_PAINT message to the application. 发生这种情况时,Windows会将WM_PAINT消息发送到应用程序。 You could check whether or not the number of WM_PAINT messages received matches the number of paintEvent calls, but I doubt Qt adds any significant overhead. 您可以检查收到的WM_PAINT消息的数量是否与paintEvent调用的数量匹配,但是我怀疑Qt是否会增加任何重大开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM