4

I would like to measure which events in my application take long time to execute in main thread (blocking GUI) or at least if there are any that take more than, lets say, 10msec. I obviously use threading and concurrency for tasks that take a long time, but it's sometimes hard to draw the line between what to put in other threads and what can stay with GUI. Especially with app that runs on multiple OSes and both new and few-years old hardware.

I looked at QApplication (and QCoreApplication) but it doesn't have any "processSingleEvent" kind of function which I cloud easily override and wrap with time measurement. Event filters also doesn't do the trick because AFAIU there is no way to get a notification after event is processed.

I thought that I could call QApplication::processEvents manually (without ever invoking exec), but again it doesn't give single-event granularity and, as I read, it doesn't handle destroy events.

I looked at QCoreApplication::exec implementation, and saw that it uses QEventLoop internally, so if I wanted to add my special code to original implementation I would have to reimplement both QApplication and QEventLoop copying a lot of code from Qt source...

Edit: the question obviously is: How to measure event handling time in possibly simple and "clean" way?

4

1 回答 1

7

覆盖bool QCoreApplication::notify (QObject * receiver, QEvent * event)

class MyApplication : public QApplication
{
    QElapsedTimer t;
public:
    MyApplication(int& argc, char ** argv) : QApplication(argc, argv) { }
    virtual ~MyApplication() { }

    virtual bool notify(QObject* receiver, QEvent* event)
    {
        t.start();
        bool ret = QApplication::notify(receiver, event);
        if(t.elapsed() > 10)
            qDebug("processing event type %d for object %s took %dms",
                (int)event->type(), receiver->objectName().toLocal8Bit().data(),
                (int)t.elapsed());
        return ret;
    }
};

int main(int argc, char *argv[])
{
    MyApplication a(argc, argv);

    ...

这也恰好是捕获所有类型异常处理的地方。

于 2013-02-27T01:29:10.053 回答