1、概述
源码放在文章末尾
该项目实现了可以显示帧率的控件,项目demo演示如下所示:
、
项目部分代码如下所示:
#ifndef FPSITEM_H
#define FPSITEM_H
#include <QQuickItem>
class FpsItem : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(int fps READ fps NOTIFY fpsChanged)
public:
FpsItem(QQuickItem *parent = nullptr);
int fps() const;
signals:
void fpsChanged();
private:
int m_fps = 0;
int m_frameCount = 0;
};
#endif // FPSITEM_H
#include "fpsitem.h"
#include <QQuickWindow>
#include <QTimer>
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;
}