From 04f3e6c541d8f932d8a1625d1c542df29d34bd8c Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Wed, 7 Jun 2023 09:16:34 +0300 Subject: [PATCH] replace use of QTime as timer with QElapsedTimer Signed-off-by: Ivailo Monev --- src/gui/painting/qbackingstore.cpp | 14 ++++++--- .../qabstractslider/tst_qabstractslider.cpp | 8 ++--- tests/auto/qatomicint/tst_qatomicint.cpp | 2 +- .../qcoreapplication/tst_qcoreapplication.cpp | 2 +- tests/auto/qmutex/tst_qmutex.cpp | 6 ++-- tests/auto/qprocess/tst_qprocess.cpp | 8 ++--- tests/auto/qthread/tst_qthread.cpp | 6 ++-- tests/auto/qthreadpool/tst_qthreadpool.cpp | 6 ++-- tests/auto/qtimer/tst_qtimer.cpp | 6 ++-- .../qgraphicsview/chiptester/chiptester.cpp | 30 +++++++++---------- .../qgraphicsview/chiptester/chiptester.h | 4 +-- .../gui/painting/qtbench/tst_qtbench.cpp | 4 +-- 12 files changed, 51 insertions(+), 45 deletions(-) diff --git a/src/gui/painting/qbackingstore.cpp b/src/gui/painting/qbackingstore.cpp index 8f3be92e2..d16f61437 100644 --- a/src/gui/painting/qbackingstore.cpp +++ b/src/gui/painting/qbackingstore.cpp @@ -29,6 +29,7 @@ #include "qpaintengine.h" #include "qgraphicsproxywidget.h" #include "qthread.h" +#include "qelapsedtimer.h" #include "qwidget_p.h" #include "qwindowsurface_p.h" #include "qapplication_p.h" @@ -53,21 +54,26 @@ static inline void qt_flush(QWidget *widget, const QRegion ®ion, QWindowSurfa //using this FPS when you have > 1 windowsurface can give you inaccurate FPS static const bool fpsDebug = qgetenv("QT_DEBUG_FPS").toInt(); if (fpsDebug) { - static QTime time = QTime::currentTime(); + static QElapsedTimer time; static int frames = 0; frames++; - if(time.elapsed() > 5000) { + if (!time.isValid()) { + time.start(); + } + + if (time.elapsed() > 5000) { double fps = double(frames * 1000) /time.restart(); fprintf(stderr,"FPS: %.1f\n",fps); frames = 0; } } - if (widget != tlw) + if (widget != tlw) { windowSurface->flush(widget, region, tlwOffset + widget->mapTo(tlw, QPoint())); - else + } else { windowSurface->flush(widget, region, tlwOffset); + } } #ifndef QT_NO_DEBUG diff --git a/tests/auto/qabstractslider/tst_qabstractslider.cpp b/tests/auto/qabstractslider/tst_qabstractslider.cpp index 1abc599f3..0d99068f1 100644 --- a/tests/auto/qabstractslider/tst_qabstractslider.cpp +++ b/tests/auto/qabstractslider/tst_qabstractslider.cpp @@ -75,7 +75,7 @@ private slots: void setRepeatAction(); private: - void waitUntilTimeElapsed(const QTime& t, int ms); + void waitUntilTimeElapsed(const QElapsedTimer& t, int ms); QWidget *topLevel; Slider *slider; @@ -1161,11 +1161,11 @@ void tst_QAbstractSlider::setValue() QVERIFY(sliderMovedTimeStamp < valueChangedTimeStamp); } -void tst_QAbstractSlider::waitUntilTimeElapsed(const QTime& t, int ms) +void tst_QAbstractSlider::waitUntilTimeElapsed(const QElapsedTimer& t, int ms) { const int eps = 80; while (t.elapsed() < ms + eps) - QTest::qWait(qMax(ms - t.elapsed() + eps, 25)); + QTest::qWait(qMax(ms - t.elapsed() + eps, qint64(25))); } void tst_QAbstractSlider::setRepeatAction() @@ -1181,7 +1181,7 @@ void tst_QAbstractSlider::setRepeatAction() QCOMPARE(spy.count(), 0); QCOMPARE(slider->value(), 55); - QTime t; + QElapsedTimer t; t.start(); QTest::qWait(300); QCOMPARE(spy.count(), 0); diff --git a/tests/auto/qatomicint/tst_qatomicint.cpp b/tests/auto/qatomicint/tst_qatomicint.cpp index ea2029662..1fd5871e3 100644 --- a/tests/auto/qatomicint/tst_qatomicint.cpp +++ b/tests/auto/qatomicint/tst_qatomicint.cpp @@ -498,7 +498,7 @@ void tst_QAtomicInt::fetchAndAdd() void tst_QAtomicInt::testAndSet_loop() { - QTime stopWatch; + QElapsedTimer stopWatch; stopWatch.start(); int iterations = 10000000; diff --git a/tests/auto/qcoreapplication/tst_qcoreapplication.cpp b/tests/auto/qcoreapplication/tst_qcoreapplication.cpp index 962a8041c..e9c54c807 100644 --- a/tests/auto/qcoreapplication/tst_qcoreapplication.cpp +++ b/tests/auto/qcoreapplication/tst_qcoreapplication.cpp @@ -506,7 +506,7 @@ void tst_QCoreApplication::processEventsAlwaysSendsPostedEvents() QCoreApplication app(argc, argv); ProcessEventsAlwaysSendsPostedEventsObject object; - QTime t; + QElapsedTimer t; t.start(); int i = 1; do { diff --git a/tests/auto/qmutex/tst_qmutex.cpp b/tests/auto/qmutex/tst_qmutex.cpp index 627970af2..c9c1ff374 100644 --- a/tests/auto/qmutex/tst_qmutex.cpp +++ b/tests/auto/qmutex/tst_qmutex.cpp @@ -87,7 +87,7 @@ void tst_QMutex::tryLock() testsTurn.release(); threadsTurn.acquire(); - QTime timer; + QElapsedTimer timer; timer.start(); QVERIFY(!normalMutex.tryLock(1000)); QVERIFY(timer.elapsed() >= 1000); @@ -227,7 +227,7 @@ enum { one_minute = 60 * 1000, threadCount = 10 }; class StressTestThread : public QThread { - QTime t; + QElapsedTimer t; public: static QAtomicInt lockCount; static QAtomicInt sentinel; @@ -279,7 +279,7 @@ public: void run() { - QTime t; + QElapsedTimer t; t.start(); do { if (mutex.tryLock()) diff --git a/tests/auto/qprocess/tst_qprocess.cpp b/tests/auto/qprocess/tst_qprocess.cpp index 5435c67a8..8386d65e4 100644 --- a/tests/auto/qprocess/tst_qprocess.cpp +++ b/tests/auto/qprocess/tst_qprocess.cpp @@ -385,7 +385,7 @@ void tst_QProcess::echoTest() msgStartProcessFailed(binary, process->errorString()).constData()); process->write(input); - QTime stopWatch; + QElapsedTimer stopWatch; stopWatch.start(); do { QVERIFY(process->isOpen()); @@ -440,7 +440,7 @@ void tst_QProcess::echoTest2() QSignalSpy spy1(process, SIGNAL(readyReadStandardOutput())); QSignalSpy spy2(process, SIGNAL(readyReadStandardError())); - QTime stopWatch; + QElapsedTimer stopWatch; stopWatch.start(); forever { QTestEventLoop::instance().enterLoop(1); @@ -486,7 +486,7 @@ void tst_QProcess::echoTest_performance() QVERIFY(process.waitForStarted()); - QTime stopWatch; + QElapsedTimer stopWatch; stopWatch.start(); qint64 totalBytes = 0; @@ -1785,7 +1785,7 @@ void tst_QProcess::fileWriterProcess() for (int i = 0; i < 5000; ++i) stdinStr += QString::fromLatin1("%1 -- testing testing 1 2 3\n").arg(i); - QTime stopWatch; + QElapsedTimer stopWatch; stopWatch.start(); do { QFile::remove("fileWriterProcess.txt"); diff --git a/tests/auto/qthread/tst_qthread.cpp b/tests/auto/qthread/tst_qthread.cpp index 483979e98..d376b3e87 100644 --- a/tests/auto/qthread/tst_qthread.cpp +++ b/tests/auto/qthread/tst_qthread.cpp @@ -223,14 +223,14 @@ public: SleepType sleepType; int interval; - int elapsed; // result, in *MILLISECONDS* + qint64 elapsed; // result, in *MILLISECONDS* void run() { QMutexLocker locker(&mutex); elapsed = 0; - QTime time; + QElapsedTimer time; time.start(); switch (sleepType) { case Second: @@ -938,7 +938,7 @@ void tst_QThread::adoptThreadExitWithActiveTimer() void tst_QThread::stressTest() { - QTime t; + QElapsedTimer t; t.start(); while (t.elapsed() < one_minute) { Current_Thread t; diff --git a/tests/auto/qthreadpool/tst_qthreadpool.cpp b/tests/auto/qthreadpool/tst_qthreadpool.cpp index ca7283119..cb5e57159 100644 --- a/tests/auto/qthreadpool/tst_qthreadpool.cpp +++ b/tests/auto/qthreadpool/tst_qthreadpool.cpp @@ -826,7 +826,7 @@ void tst_QThreadPool::tryStartCount() void tst_QThreadPool::waitForDone() { - QTime total, pass; + QElapsedTimer total, pass; total.start(); QThreadPool threadPool; @@ -881,7 +881,7 @@ void tst_QThreadPool::waitForDoneTimeout() void tst_QThreadPool::destroyingWaitsForTasksToFinish() { - QTime total, pass; + QElapsedTimer total, pass; total.start(); while (total.elapsed() < 10000) { @@ -935,7 +935,7 @@ void tst_QThreadPool::stressTest() } }; - QTime total; + QElapsedTimer total; total.start(); while (total.elapsed() < 30000) { Task t; diff --git a/tests/auto/qtimer/tst_qtimer.cpp b/tests/auto/qtimer/tst_qtimer.cpp index 8b7356928..2f505c59a 100644 --- a/tests/auto/qtimer/tst_qtimer.cpp +++ b/tests/auto/qtimer/tst_qtimer.cpp @@ -390,7 +390,7 @@ public: QBasicTimer m_timer; int m_interval; - QTime m_startedTime; + QElapsedTimer m_startedTime; QEventLoop eventLoop; inline RestartedTimerFiresTooSoonObject() @@ -416,7 +416,7 @@ public: m_timer.stop(); - int elapsed = m_startedTime.elapsed(); + qint64 elapsed = m_startedTime.elapsed(); if (elapsed < m_interval / 2) { // severely too early! @@ -455,7 +455,7 @@ public slots: void longLastingSlot() { // Don't use timers for this, because we are testing them. - QTime time; + QElapsedTimer time; time.start(); while (time.elapsed() < 200) { for (int c = 0; c < 100000; c++) {} // Mindless looping. diff --git a/tests/benchmarks/gui/graphicsview/qgraphicsview/chiptester/chiptester.cpp b/tests/benchmarks/gui/graphicsview/qgraphicsview/chiptester/chiptester.cpp index 68af7fba6..334288210 100644 --- a/tests/benchmarks/gui/graphicsview/qgraphicsview/chiptester/chiptester.cpp +++ b/tests/benchmarks/gui/graphicsview/qgraphicsview/chiptester/chiptester.cpp @@ -63,26 +63,26 @@ void ChipTester::paintEvent(QPaintEvent *event) { QGraphicsView::paintEvent(event); if (++npaints == 50) - eventLoop.quit(); + eventLoop.quit(); } void ChipTester::timerEvent(QTimerEvent *) { switch (operation) { - case Rotate360: - rotate(1); - break; - case ZoomInOut: { - qreal s = 0.05 + (npaints / 20.0); - setTransform(QTransform().scale(s, s)); - break; - } - case Translate: { - int offset = horizontalScrollBar()->minimum() - + (npaints % (horizontalScrollBar()->maximum() - horizontalScrollBar()->minimum())); - horizontalScrollBar()->setValue(offset); - break; - } + case Rotate360: + rotate(1); + break; + case ZoomInOut: { + qreal s = 0.05 + (npaints / 20.0); + setTransform(QTransform().scale(s, s)); + break; + } + case Translate: { + int offset = horizontalScrollBar()->minimum() + + (npaints % (horizontalScrollBar()->maximum() - horizontalScrollBar()->minimum())); + horizontalScrollBar()->setValue(offset); + break; + } } } diff --git a/tests/benchmarks/gui/graphicsview/qgraphicsview/chiptester/chiptester.h b/tests/benchmarks/gui/graphicsview/qgraphicsview/chiptester/chiptester.h index 2056a77aa..a39e034d7 100644 --- a/tests/benchmarks/gui/graphicsview/qgraphicsview/chiptester/chiptester.h +++ b/tests/benchmarks/gui/graphicsview/qgraphicsview/chiptester/chiptester.h @@ -25,7 +25,7 @@ #include #include #include -#include +#include class ChipTester : public QGraphicsView { @@ -54,7 +54,7 @@ private: int npaints; int timerId; QEventLoop eventLoop; - QTime stopWatch; + QElapsedTimer stopWatch; Operation operation; }; diff --git a/tests/benchmarks/gui/painting/qtbench/tst_qtbench.cpp b/tests/benchmarks/gui/painting/qtbench/tst_qtbench.cpp index cfb356dba..c2a1e6189 100644 --- a/tests/benchmarks/gui/painting/qtbench/tst_qtbench.cpp +++ b/tests/benchmarks/gui/painting/qtbench/tst_qtbench.cpp @@ -37,7 +37,7 @@ public: qreal result() const { return m_result; } public: - QTime timer; + QElapsedTimer timer; Benchmark *m_benchmark; @@ -70,7 +70,7 @@ void BenchWidget::paintEvent(QPaintEvent *) ++m_iteration; - uint currentElapsed = timer.isNull() ? 0 : timer.elapsed(); + uint currentElapsed = !timer.isValid() ? 0 : timer.elapsed(); timer.restart(); m_total += currentElapsed;