Compare commits

...

4 Commits

Author SHA1 Message Date
Hendrik Schutter 41900f7d93 benchmark with rectangle 2020-10-12 12:13:15 +02:00
Hendrik Schutter 620d7df720 better fps counter 2020-10-12 10:48:41 +02:00
Hendrik Schutter 9e3db3865a added fps counter 2020-10-11 22:44:19 +02:00
Hendrik Schutter 66a18ca17a added animation 2020-10-11 22:12:32 +02:00
10 changed files with 222 additions and 177 deletions

View File

@ -1,6 +1,4 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
QT += quick
CONFIG += c++11
@ -9,16 +7,21 @@ CONFIG += c++11
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
frequencymonitor.cpp \
main.cpp
HEADERS += \
mainwindow.h
RESOURCES += qml.qrc
FORMS += \
mainwindow.ui
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
frequencymonitor.h

View File

@ -0,0 +1,20 @@
import QtQuick 2.0
Rectangle {
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
width:100
height:100
color:Qt.rgba(Math.random(),Math.random(),Math.random(),1);
NumberAnimation on x {loops: Animation.Infinite; from: randomInteger(0, 1920); to: randomInteger(0, 1920); duration: 500 }
NumberAnimation on y {loops: Animation.Infinite; from: randomInteger(0, 900); to: randomInteger(0, 900); duration: 500 }
}

View File

@ -0,0 +1,65 @@
#include "frequencymonitor.h"
#include <QQuickWindow>
FrequencyMonitor::FrequencyMonitor(QQuickItem *parent) :
QQuickItem(parent),
m_counter(0),
m_refreshPeriod(1000)
{
connect(this, &FrequencyMonitor::windowChanged, this, &FrequencyMonitor::handleWindowChanged);
}
int FrequencyMonitor::fps() const
{
return m_fps;
}
void FrequencyMonitor::setFps(int fps)
{
if (m_fps == fps)
return;
m_fps = fps;
emit fpsChanged();
}
int FrequencyMonitor::refreshPeriod() const
{
return m_refreshPeriod;
}
void FrequencyMonitor::setRefreshPeriod(int msec)
{
if (m_refreshPeriod == msec)
return;
m_refreshPeriod = msec;
emit refreshPeriodChanged();
}
void FrequencyMonitor::handleAfterRendering()
{
const int elapsedMsec = m_time.elapsed();
m_counter++;
if (elapsedMsec >= m_refreshPeriod)
{
setFps(m_counter / (elapsedMsec / 1000.0));
m_counter = 0;
m_time.restart();
}
}
void FrequencyMonitor::handleWindowChanged(QQuickWindow *window)
{
if (m_windowConnection)
this->disconnect(m_windowConnection);
if (window)
{
m_time.restart();
m_windowConnection = connect(window, &QQuickWindow::afterRendering, this, &FrequencyMonitor::handleAfterRendering);
}
}

View File

@ -0,0 +1,42 @@
#ifndef FREQUENCYMONITOR_H
#define FREQUENCYMONITOR_H
#include <QQuickItem>
#include <QTime>
#include <QMetaObject>
class QQuickWindow;
class FrequencyMonitor : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(int fps READ fps NOTIFY fpsChanged)
Q_PROPERTY(int refreshPeriod READ refreshPeriod WRITE setRefreshPeriod NOTIFY refreshPeriodChanged)
public:
explicit FrequencyMonitor(QQuickItem *parent = 0);
int fps() const;
void setFps(int fps);
// by default refresh one time per 1000 msec
int refreshPeriod() const;
void setRefreshPeriod(int msec);
signals:
void fpsChanged();
void refreshPeriodChanged();
public slots:
void handleAfterRendering();
void handleWindowChanged(QQuickWindow * window);
protected:
QMetaObject::Connection m_windowConnection;
QTime m_time;
int m_fps;
int m_counter;
int m_refreshPeriod;
};
#endif // FREQUENCYMONITOR_H

View File

@ -1,11 +1,26 @@
#include "mainwindow.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "frequencymonitor.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<FrequencyMonitor>("be.mindgoo.tools", 0, 1, "FrequencyMonitor");
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}

View File

@ -0,0 +1,56 @@
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.3
import be.mindgoo.tools 0.1
Window {
id: rootwin
width: 1920
height: 1080
visible: true
property var elements: 0
FrequencyMonitor {
id: monitor
refreshPeriod: 500
}
function doSomething(){
rootwin.elements++
var component = Qt.createComponent("Rect.qml");
var rect = component.createObject(rootwin,{"x":0,"y":0});
if(rect !== null ) {
rect.name = "Test";
rect.x = Math.floor(Math.random() * 100 );
rect.y = Math.floor(Math.random() * 100 );
}
}
Button{
id: doBtn
text: "do"
x: 20
y: 1000
onClicked: rootwin.doSomething()
}
Text {
id: fpsCounter
x: 200
y: 1000
text: "FPS: " + monitor.fps
}
Text {
id: elementsCounter
x: 300
y: 1000
text: "Elements: " + rootwin.elements
}
}
/*##^##
Designer {
D{i:0;formeditorZoom:2}
}
##^##*/

View File

@ -1,15 +0,0 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}

View File

@ -1,21 +0,0 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

View File

@ -1,126 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="gridLayoutWidget">
<property name="geometry">
<rect>
<x>140</x>
<y>70</y>
<width>281</width>
<height>251</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>20</number>
</property>
<property name="topMargin">
<number>20</number>
</property>
<property name="rightMargin">
<number>20</number>
</property>
<property name="bottomMargin">
<number>20</number>
</property>
<property name="horizontalSpacing">
<number>90</number>
</property>
<property name="verticalSpacing">
<number>6</number>
</property>
<item row="4" column="0">
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>CheckBox</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLineEdit" name="lineEdit">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="font">
<font>
<pointsize>9</pointsize>
</font>
</property>
<property name="text">
<string>Label</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QDateEdit" name="dateEdit"/>
</item>
<item row="7" column="0">
<widget class="QSlider" name="horizontalSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QComboBox" name="comboBox">
<item>
<property name="text">
<string>First</string>
</property>
</item>
<item>
<property name="text">
<string>Second</string>
</property>
</item>
</widget>
</item>
<item row="3" column="0">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QProgressBar" name="progressBar">
<property name="value">
<number>24</number>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>Rect.qml</file>
</qresource>
</RCC>