问题描述
我有一些问题。希望有人能帮助我。我有一个Qwidget1和Qwidget2。 Qwidget1有一个小部件,可以升级到Qwidget2。 Qwidget1和Qwidget2都有paintEvent。我在Qwidget1的paint事件中写了qDebug()<<Update;。当我运行项目时,我看到一个单词更新已被打印很多次。那么为什么Qwidget1会多次执行绘制事件。我怎么能修复它,只是在第一次显示Qwidget1时以及当我调用update时执行paint事件。
I have some problems. Hope anyone can help me. I have a Qwidget1 and Qwidget2. Qwidget1 have a widget that promote to Qwidget2. Both Qwidget1 and Qwidget2 have paintEvent. I have writed "qDebug()<< "Update"; " in paint event of Qwidget1. 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. How can I fix it, just execute paint event when show Qwidget1 at the first time and when I call update.
推荐答案
绘制事件是重新绘制全部或部分窗口小部件的请求。这可能是由于以下原因之一:
重绘()或更新()被调用,
小部件被遮挡并且有现在已被发现,或者
许多其他原因。
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,
the widget was obscured and has now been uncovered, or
many other reasons.
因此可能有很多原因导致小部件(或部分它必须重新粉刷(上面没有提到的是调整大小和移动窗口)。如果您确定这不是由您的代码提供的(例如,通过调用小部件的 repaint()
函数而不是 update()
函数或执行可能触发绘制事件的其他任务)您将不得不忍受这种行为。
如果遇到性能问题,你应该优化你的绘图实现(例如,只更新传递区域/ rect所覆盖的那些部分)。
如果你真的想知道事件可能是什么原因,你还应该检查区域/矩形以查看是否只有部分或完整的小部件必须更新和挖掘深入到Qt来源。
So there may be many reasons why the widget (or portions of it) must be repainted (among the not mentioned ones above are resizing and moving of the window). If you are sure that this is not sourced by your code (e.g. by calling the widget's repaint()
function instead of the update()
function or perfoming other tasks that might trigger the paint event) you will have to live with the behaviour.
If you have performance issues, you should optimise your paint implementation (e.g. by updating only those portions that are covered by the passed region / rect).
If you really want to know what might be the cause of the events, you should also check the region / rect to see if only portions or the complete widget has to be updated and dig deep into the Qt sources.
这篇关于Qt创建者:Qwidget的痛苦尝试。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!