#include#include void myFunction() { QElapsedTimer timer; timer.start(); // Perform some task here qDebug() << "Execution time:" << timer.elapsed() << "milliseconds"; }
#includeIn this example, we create a QLabel with a simple text and show it. We also create a QElapsedTimer object and a QTimer object that is set to trigger every 16 milliseconds (60 fps). We connect the timeout signal of the QTimer to a lambda function that calculates the elapsed time in seconds and the frame rate using the `nsecsElapsed()` method. We update the text of the QLabel with the calculated frame rate and start the timer again. Finally, we start the event loop using `app.exec()`. Package library: Qt Core (QtCore)#include #include #include int main(int argc, char *argv[]) { QApplication app(argc, argv); QLabel label("Hello, world!"); label.show(); QElapsedTimer timer; QTimer frameTimer; QObject::connect(&frameTimer, &QTimer::timeout, [&]() { float elapsedSeconds = timer.nsecsElapsed() / 1000000000.0; float fps = 1.0 / elapsedSeconds; label.setText(QString("FPS: %1").arg(fps, 0, 'f', 1)); timer.start(); }); frameTimer.start(16); // 60 fps return app.exec(); }