luf*_*ffy 8 c++ linux qt frame-rate qml
是否有一种"简单"的方式来显示QML/c ++应用程序中的FPS(帧速率).所有动画和视图都在QML中完成,应用程序逻辑在c ++中.
我已经尝试QML_SHOW_FRAMERATE在启动应用程序之前在Linux中进行设置,但它没有帮助:
export QML_SHOW_FRAMERATE=1
Run Code Online (Sandbox Code Playgroud)
您必须创建自己的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();
}
Run Code Online (Sandbox Code Playgroud)
QML:
FPSText{
id: fps_text
x:0
y: 0;
width: 200
height: 100
Text {
anchors.centerIn: parent
text: fps_text.fps.toFixed(2)
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过快速搜索在Internet上获得任何其他实现.
QML FPS Counter,不影响性能。
\n\nQNanoPainter和 qt-labs 中的其他人的项目正在使用 QML 项的动画刷新来创建 FPS 计数器。这很容易完成,附上一个使用此技术的项目(从QNanoPainter FPS 计数器修改)。
\n\nFps项目代码:
\n\nimport QtQuick 2.0\nimport QtQuick.Window 2.2\n\nRectangle {\n id: root\n property int frameCounter: 0\n property int frameCounterAvg: 0\n property int counter: 0\n property int fps: 0\n property int fpsAvg: 0\n\n readonly property real dp: Screen.pixelDensity * 25.4/160\n\n color: "black"\n width: childrenRect.width + 10*dp;\n height: childrenRect.height + 10*dp;\n\n Image {\n id: spinnerImage\n anchors.verticalCenter: parent.verticalCenter\n x: 4 * dp\n width: 36 * dp\n height: width\n source: "images/spinner.png"\n NumberAnimation on rotation {\n from:0\n to: 360\n duration: 800\n loops: Animation.Infinite\n }\n onRotationChanged: frameCounter++;\n }\n\n Text {\n anchors.left: spinnerImage.right\n anchors.leftMargin: 8 * dp\n anchors.verticalCenter: spinnerImage.verticalCenter\n color: "#c0c0c0"\n font.pixelSize: 18 * dp\n text: "\xc3\x98 " + root.fpsAvg + " | " + root.fps + " fps"\n }\n\n Timer {\n interval: 2000\n repeat: true\n running: true\n onTriggered: {\n frameCounterAvg += frameCounter;\n root.fps = frameCounter/2;\n counter++;\n frameCounter = 0;\n if (counter >= 3) {\n root.fpsAvg = frameCounterAvg/(2*counter)\n frameCounterAvg = 0;\n counter = 0;\n }\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n将其用作:
\n\nimport QtQuick 2.9\nimport QtQuick.Window 2.2\n\nWindow {\n visible: true\n width: 640\n height: 480\n title: qsTr("Hello World")\n\n FpsItem {\n id: fpsItem\n anchors.centerIn: parent\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n