Skip to content
Snippets Groups Projects
fpsitem.cpp 582 B
Newer Older
Amos Yang's avatar
Amos Yang committed
#include <QQuickWindow>
#include <QTimer>
Amos Yang's avatar
Amos Yang committed
#include "fpsitem.h"
Amos Yang's avatar
Amos Yang committed

FpsItem::FpsItem(QQuickItem *parent)
    : QQuickItem(parent)
{
    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, [this]{ m_fps = m_frameCount; m_frameCount = 0; emit fpsChanged(); });
    connect(this, &QQuickItem::windowChanged, this, [this]{
        if (window())
            connect(window(), &QQuickWindow::afterRendering, this
                    , [this]{ m_frameCount++; }, Qt::DirectConnection);
    });
    timer->start(1000);
}

int FpsItem::fps() const
{
    return m_fps;
}