我们在使用Qt的时候,在刷新界面到底是使用update()还是repaint()?这两者有什么区别?
update()会在多次调用以后,系统会在合适的时机重绘一次,而repaint()系统会立即响应,调用多少次repaint就会重绘多少次。
以下源码来自Qt5.1.1版本
update()源码:
void QWidget::update()
{
update(rect());
}
/*! \fn void QWidget::update(int x, int y, int w, int h)
\overload
This version updates a rectangle (\a x, \a y, \a w, \a h) inside
the widget.
*/
/*!
\overload
This version updates a rectangle \a rect inside the widget.
*/
void QWidget::update(const QRect &rect)
{
if (!isVisible() || !updatesEnabled())
return;
QRect r = rect & QWidget::rect();
if (r.isEmpty())
return;
if (testAttribute(Qt::WA_WState_InPaintEvent)) {
QApplication::postEvent(this, new QUpdateLaterEvent(r));
return;
}
if (hasBackingStoreSupport()) {
#ifdef Q_WS_MAC
if (qt_widget_private(this)->isInUnifiedToolbar) {
qt_widget_private(this)->unifiedSurface->renderToolbar(this, true);
return;
}
#endif // Q_WS_MAC
QTLWExtra *tlwExtra = window()->d_func()->maybeTopData();
if (tlwExtra && !tlwExtra->inTopLevelResize && tlwExtra->backingStore)
tlwExtra->backingStoreTracker->markDirty(r, this);
} else {
d_func()->repaint_sys(r);
}
}主要关注

本文深入探讨了Qt中update()和repaint()的区别及其内部实现原理。通过对比两种方法的源码,揭示了它们如何影响界面刷新时机及效率,有助于开发者更高效地进行UI更新。
最低0.47元/天 解锁文章
1288

被折叠的 条评论
为什么被折叠?



