Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

611
Views
Mostrar FPS en QML

¿Existe una forma "simple" de mostrar el FPS (velocidad de fotogramas) en una aplicación QML/c++? Todas las animaciones y vistas se realizan en QML y la lógica de la aplicación está en C++.

Ya intenté configurar QML_SHOW_FRAMERATE en Linux antes de iniciar la aplicación, pero no funcionó:

 export QML_SHOW_FRAMERATE=1
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Tienes que crear tu propio FPS QQuickItem (o QQuickPaintedItem) y registrarte en tu main.cpp para que esté disponible en tu código QML.

Aquí un ejemplo.

 class FPSText: public QQuickPaintedItem { Q_OBJECT Q_PROPERTY(int fps READ fps NOTIFY fpsChanged) public: FPSText(QQuickItem *parent = 0); ~FPSText(); void paint(QPainter *); Q_INVOKABLE int fps()const; signals: void fpsChanged(int); private: void recalculateFPS(); int _currentFPS; int _cacheCount; QVector<qint64> _times; }; FPSText::FPSText(QQuickItem *parent): QQuickPaintedItem(parent), _currentFPS(0), _cacheCount(0) { _times.clear(); setFlag(QQuickItem::ItemHasContents); } FPSText::~FPSText() { } void FPSText::recalculateFPS() { qint64 currentTime = QDateTime::currentDateTime().toMSecsSinceEpoch(); _times.push_back(currentTime); while (_times[0] < currentTime - 1000) { _times.pop_front(); } int currentCount = _times.length(); _currentFPS = (currentCount + _cacheCount) / 2; qDebug() << _currentFPS; if (currentCount != _cacheCount) fpsChanged(_currentFPS); _cacheCount = currentCount; } int FPSText::fps()const { return _currentFPS; } void FPSText::paint(QPainter *painter) { recalculateFPS(); //qDebug() << __FUNCTION__; QBrush brush(Qt::yellow); painter->setBrush(brush); painter->setPen(Qt::NoPen); painter->setRenderHint(QPainter::Antialiasing); painter->drawRoundedRect(0, 0, boundingRect().width(), boundingRect().height(), 0, 0); update(); }

qml:

 FPSText{ id: fps_text x:0 y: 0; width: 200 height: 100 Text { anchors.centerIn: parent text: fps_text.fps.toFixed(2) } }

Puede obtener cualquier otra implementación en Internet con una búsqueda rápida.

over 4 years ago · Santiago Trujillo Report

0

Contador QML FPS, sin afectar el rendimiento.

El proyecto de QNanoPainter y otros en qt-labs están utilizando la actualización de una animación de un elemento QML para crear un contador de FPS. Es muy fácil hacerlo, adjunto un proyecto que usa esta técnica (modificado del contador FPS de QNanoPainter ).

Código FpsItem:

 import QtQuick 2.0 import QtQuick.Window 2.2 Rectangle { id: root property int frameCounter: 0 property int frameCounterAvg: 0 property int counter: 0 property int fps: 0 property int fpsAvg: 0 readonly property real dp: Screen.pixelDensity * 25.4/160 color: "black" width: childrenRect.width + 10*dp; height: childrenRect.height + 10*dp; Image { id: spinnerImage anchors.verticalCenter: parent.verticalCenter x: 4 * dp width: 36 * dp height: width source: "images/spinner.png" NumberAnimation on rotation { from:0 to: 360 duration: 800 loops: Animation.Infinite } onRotationChanged: frameCounter++; } Text { anchors.left: spinnerImage.right anchors.leftMargin: 8 * dp anchors.verticalCenter: spinnerImage.verticalCenter color: "#c0c0c0" font.pixelSize: 18 * dp text: "Ø " + root.fpsAvg + " | " + root.fps + " fps" } Timer { interval: 2000 repeat: true running: true onTriggered: { frameCounterAvg += frameCounter; root.fps = frameCounter/2; counter++; frameCounter = 0; if (counter >= 3) { root.fpsAvg = frameCounterAvg/(2*counter) frameCounterAvg = 0; counter = 0; } } } }

Utilizándolo como:

 import QtQuick 2.9 import QtQuick.Window 2.2 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") FpsItem { id: fpsItem anchors.centerIn: parent } }
over 4 years ago · Santiago Trujillo Report

0

Estudié un poco el tema. Usualmente usé el enfoque descrito por @Miguel Angel, pero recientemente implementé uno más simple usando la señal QQuickWindow::frameSwapped(). El código y la información se pueden encontrar aquí: https://github.com/carlonluca/lqtutils#lqtutils_uih . Es fácil de integrar en otra aplicación.

Comparé estas dos técnicas con la de @forlayo: las dos primeras parecen estar siempre de acuerdo en la medida, la otra es típicamente diferente.

Esta es la comparativa: https://youtu.be/p_y_kL85R4A .

También escribí una publicación de blog con más información: https://thebugfreeblog.blogspot.com/2021/09/measure-framerate-qt.html

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!