English 中文(简体)
Qt widget update later but when?
原标题:

I d like to know what happens exactly when I call a QWidget s update() method.
Here is the documentation:

http://doc.qt.digia.com/4.5/qwidget.html#update

This function does not cause an immediate repaint; instead it schedules a paint event for processing when Qt returns to the main event loop. This permits Qt to optimize for more speed and less flicker than a call to repaint() does.

I see from the Qt source code that a QUpdateLaterEvent is created and post with type QEvent::UpdateLater

In this part of the documentation http://doc.qt.digia.com/4.5/qevent.html

QEvent::UpdateLater: The widget should be queued to be repainted at a later time.

What does later time mean?
Are my all emited queued signals and the events in the event queue processed before the paint?

Thanks,
Gabor

最佳回答

After checking QWidget::update() source code I ve found it calls this method in src/gui/kernel/qwidget.cpp:9544 :

void QWidget::update(const QRect &rect)
{
    if (!isVisible() || !updatesEnabled() || rect.isEmpty())
        return;

    if (testAttribute(Qt::WA_WState_InPaintEvent)) {
        QApplication::postEvent(this, new QUpdateLaterEvent(rect));
        return;
    }

    if (hasBackingStoreSupport()) {
        QTLWExtra *tlwExtra = window()->d_func()->maybeTopData();
        if (tlwExtra && !tlwExtra->inTopLevelResize && tlwExtra->backingStore)
            tlwExtra->backingStore->markDirty(rect, this);
    } else {
        d_func()->repaint_sys(rect);
    }
}

As you can see the QUpdateLaterEvent is only posted if the update() is already called from inside a paintEvent() method.

You can also check QWidget::repaint(const QRect &rect) source on line 9456 - it lacks testAttribute(Qt::WA_WState_InPaintEvent) check.

EDIT

The QUpdateLaterEvent is posted as a Qt::NormalEventPriority event, so it gets processed after all other normal priority events (see src/corelib/kernel/qcoreapplication.cpp:971 and :1003). You might also want to look into compressEvent code, I haven t checked that.

So to finally answer the question: the QUpdateLaterEvent is processed after other high and normal priority events that were in queue before it was posted.

问题回答

Behavior is not documented == not guaranteed to stay the same between Qt versions. You shouldn t write code which depends on the ordering of paint events relative to other events.





相关问题
Qt: Do events get processed in order?

If I had a class A, where one of its functions does: void A::func() { emit first_signal(); emit second_signal(); } Assuming that a class B has 2 slots, one connected to first_signal, and the ...

How to determine how much free space on a drive in Qt?

I m using Qt and want a platform-independent way of getting the available free disk space. I know in Linux I can use statfs and in Windows I can use GetDiskFreeSpaceEx(). I know boost has a way, ...

Drag & drop with .ui files

I m having big troubles with drag & drop. I ve created a new Qt Designer Form Class in which I have one QListWidget and one QWidget. Now I want to enable dragging & dropping between these two ...

Link errors on Snow Leopard

I creating a small desktop application using Qt and Poco on Mac OS X Snow Leopard. Qt works fine, but once I started linking with Poco I get the following warning: ld: warning: in /Developer/SDKs/...

Showing two windows in Qt4

My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try ...

Qt equivalent of .NET data binding?

Is there an equivalent of .NET s data binding in Qt? I want to populate some combo boxes and other widgets with QStrings that refer to specific entities in my database. However, it would be cleaner ...

热门标签