我们在使用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_