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



  • Set the QML_SHOW_FRAMERATE to output the frame rate.



  • 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



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



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



  • 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



  • 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 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



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

    @
    src/declarative/util/qdeclarativeview.cpp
    @



  • 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
    void paintEvent(QPaintEvent *event);
    Q_INVOKABLE float getCurrentFPS ();
    QTime m_time;
    int m_frameCount;
    float currentFPS;@

    @ .cpp file
    void QmlApplicationViewer::paintEvent(QPaintEvent *event)
    {
    //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<QApplication> app(createApplication(argc, 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 }
    }
    

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.