在QML / c ++应用程序中是否有“简单”的方式来显示FPS(帧速率)? 所有animation和视图都是在QML中完成的,应用程序逻辑是在c ++中。
在启动应用程序之前,我已经尝试在Linux中设置QML_SHOW_FRAMERATE
,但是没有帮助:
export QML_SHOW_FRAMERATE=1
您必须创建您自己的FPS QQuickItem(或QQuickPaintedItem)并在您的main.cpp中注册,以便在您的QML代码中可用。
这里是一个例子。
class FPSText: public QQuickPaintedItem { Q_OBJECT Q_PROPERTY(int fps READ fps NOTIFY fpsChanged) public: FPSText(QQuickItem *parent = 0); ~FPSText(); void paint(QPainter *); Q_INVOKABLE int fps()const; signals: void fpsChanged(int); private: void recalculateFPS(); int _currentFPS; int _cacheCount; QVector<qint64> _times; }; FPSText::FPSText(QQuickItem *parent): QQuickPaintedItem(parent), _currentFPS(0), _cacheCount(0) { _times.clear(); setFlag(QQuickItem::ItemHasContents); } FPSText::~FPSText() { } void FPSText::recalculateFPS() { qint64 currentTime = QDateTime::currentDateTime().toMSecsSinceEpoch(); _times.push_back(currentTime); while (_times[0] < currentTime - 1000) { _times.pop_front(); } int currentCount = _times.length(); _currentFPS = (currentCount + _cacheCount) / 2; qDebug() << _currentFPS; if (currentCount != _cacheCount) fpsChanged(_currentFPS); _cacheCount = currentCount; } int FPSText::fps()const { return _currentFPS; } void FPSText::paint(QPainter *painter) { recalculateFPS(); //qDebug() << __FUNCTION__; QBrush brush(Qt::yellow); painter->setBrush(brush); painter->setPen(Qt::NoPen); painter->setRenderHint(QPainter::Antialiasing); painter->drawRoundedRect(0, 0, boundingRect().width(), boundingRect().height(), 0, 0); update(); }
QML:
FPSText{ id: fps_text x:0 y: 0; width: 200 height: 100 Text { anchors.centerIn: parent text: fps_text.fps.toFixed(2) } }
您可以通过快速搜索在Internet上获得任何其他实现。