December 3, 2010

2beers 2beers
Lab Rat
111 posts

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

December 3, 2010

blam blam
Lab Rat
58 posts

Set the QML_SHOW_FRAMERATE to output the frame rate.

December 3, 2010

digitalsurgeon digitalsurge..
Lab Rat
14 posts

like blam said already you need to set the QML_SHOW_FRAMERATE env variable

on linux for example:

export QML_SHOW_FRAMERATE=1

then run qmlviewer

December 3, 2010

moo1 moo1
Lab Rat
24 posts

Does it affect to C++ app with QML, or it’s a qmlviewer feature?

December 4, 2010

QtK QtK
Lab Rat
1045 posts

In c++ app you can use, don’t know how to use it with the qmlviewer.exe

October 28, 2011

billouparis billouparis
Lab Rat
129 posts

How can you display this framerate on a screen, in your QML Rectangle for instance, rather than in the terminal output? And can you use a timer to refresh the value only a certain amount of time by second to not sacrifice the refresh itself?

Thank you,
Bill

October 30, 2011

ZapB ZapB
Ant Farmer
1442 posts

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().

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

January 10, 2012

billouparis billouparis
Lab Rat
129 posts

I grepped the source code and never found the QML_SHOW_FRAMERATE, is there something really tricky to do to simply get the FPS displayed in a QML application?
Any new help welcome!
Bill

January 10, 2012

ZapB ZapB
Ant Farmer
1442 posts

A quick grep of the Qt source code shows it to be in:

  1. src/declarative/util/qdeclarativeview.cpp

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

January 10, 2012

billouparis billouparis
Lab Rat
129 posts

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:

  1.  .h file
  2.     void paintEvent(QPaintEvent *event);
  3.     Q_INVOKABLE float getCurrentFPS ();
  4.     QTime m_time;
  5.     int m_frameCount;
  6.     float currentFPS;

  1.  .cpp file
  2. void QmlApplicationViewer::paintEvent(QPaintEvent *event)
  3.  {
  4.      //do nothing
  5.      if (m_frameCount == 0) {
  6.           m_time.start();
  7.      } else {
  8.          currentFPS =  m_time.elapsed() / float(m_frameCount);
  9.          //qDebug()<< "FPS is %f ms\n" << m_time.elapsed() / float(m_frameCount);
  10.      }
  11.      m_frameCount++;
  12.      //qDebug()<<"Frame n°"<<m_frameCount;
  13.  
  14.      QDeclarativeView::paintEvent(event);
  15.  }
  16. float QmlApplicationViewer::getCurrentFPS ()
  17.  {
  18.      return currentFPS;
  19.  }

Then I exposed the viewer itself to the qml in the main.cpp file

  1. int main(int argc, char *argv[])
  2. {
  3.  
  4.     QScopedPointer<QApplication> app(createApplication(argc, argv));
  5.     QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());
  6.  
  7.  
  8.     viewer->rootContext()->setContextProperty("myViewer", viewer.data());

The qml file can be something like this:

  1. import QtQuick 1.0
  2.  
  3. Item {
  4.     id: firstApp
  5.     width:1024
  6.     height:768
  7.  
  8.     Image{
  9.         id: background
  10.         x:0
  11.         y:0
  12.         width:1024
  13.         height:768
  14.         source: "../../../Resources/BGTest.png"
  15.     }
  16.  
  17.     property string myText: ""
  18.     Rectangle {
  19.         x:10
  20.         y:40
  21.         width: 100
  22.         height: 100
  23.         color: "white"
  24.         Text {
  25.             x:10
  26.             y:40
  27.             font.pixelSize: 24
  28.             text: "FPS: "+myText
  29.             color: "black"
  30.         }
  31.     }
  32.  
  33.     Timer {
  34.         interval: 1000
  35.         running: true
  36.         repeat: true
  37.         onTriggered: {
  38.            
  39.             myText = myViewer.getCurrentFPS()
  40.         }
  41.     }
  42.  
  43.  
  44.     SequentialAnimation {
  45.         running: true
  46.         loops: Animation.Infinite
  47.  
  48.         NumberAnimation { target: background; property: "x"; to: 1024; duration: 2000 }
  49.         PauseAnimation { duration: 5000 }
  50.         NumberAnimation { target: background; property: "x"; to: 0; duration: 2000 }
  51.     }
  52.  
  53.  
  54. }

 
  ‹‹ Problem with plugins      Dynamic text using QString::arg() in QML ››

You must log in to post a reply. Not a member yet? Register here!