QML and frame rate
Hello everyone.
Is there any way I can see the frame rate of an QML applicaiton. I looked in QML viewer and didn’t find anything. if I create a c++ program and use QDeclarative, is there any way I can print the framerate?
Thanks
9 replies
Grep the source code of Qt for QML_SHOW_FRAMERATE and see how it’s done there then replicate it yourself as I suspect it may use some private API (but I’ve not looked so I can’t be certain). Once you have calculated the framerate just expose it to QML like any other data by exposing it as a property on a QObject and calling QDeclarativeContext::setContextProperty().
I realized I don’t have all source files apparently, so I did the following:
I finally overloaded the paintEvent method from the QDeclarativeView subclass QApplicationViewer as follows:
- .h file
- Q_INVOKABLE float getCurrentFPS ();
- QTime m_time;
- int m_frameCount;
- float currentFPS;
- .cpp file
- {
- //do nothing
- if (m_frameCount == 0) {
- m_time.start();
- } else {
- currentFPS = m_time.elapsed() / float(m_frameCount);
- //qDebug()<< "FPS is %f ms\n" << m_time.elapsed() / float(m_frameCount);
- }
- m_frameCount++;
- //qDebug()<<"Frame n°"<<m_frameCount;
- QDeclarativeView::paintEvent(event);
- }
- float QmlApplicationViewer::getCurrentFPS ()
- {
- return currentFPS;
- }
Then I exposed the viewer itself to the qml in the main.cpp file
- int main(int argc, char *argv[])
- {
- QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());
- viewer->rootContext()->setContextProperty("myViewer", viewer.data());
The qml file can be something like this:
- import QtQuick 1.0
- Item {
- id: firstApp
- width:1024
- height:768
- Image{
- id: background
- x:0
- y:0
- width:1024
- height:768
- source: "../../../Resources/BGTest.png"
- }
- property string myText: ""
- Rectangle {
- x:10
- y:40
- width: 100
- height: 100
- color: "white"
- Text {
- x:10
- y:40
- font.pixelSize: 24
- text: "FPS: "+myText
- color: "black"
- }
- }
- Timer {
- interval: 1000
- running: true
- repeat: true
- onTriggered: {
- myText = myViewer.getCurrentFPS()
- }
- }
- SequentialAnimation {
- running: true
- loops: Animation.Infinite
- NumberAnimation { target: background; property: "x"; to: 1024; duration: 2000 }
- PauseAnimation { duration: 5000 }
- NumberAnimation { target: background; property: "x"; to: 0; duration: 2000 }
- }
- }
You must log in to post a reply. Not a member yet? Register here!





