the data class
#ifndef PIECE_H
#define PIECE_H
#include <QtCore/qobjectdefs.h>
#include <QtCore/qtmetamacros.h>
class piece
{
Q_GADGET
Q_PROPERTY(float x READ x WRITE setX)
Q_PROPERTY(float y READ y WRITE setY)
public:
piece() : m_x(0.0f), m_y(0.0f) {}
piece(const piece &) = default;
piece(piece &&) = default;
piece &operator=(const piece &) = default;
piece &operator=(piece &&) = default;
explicit piece(const float &x, const float &y) : m_x(x), m_y(y) {}
void setX(float newX) {
if (!qFuzzyCompare(newX, m_x))
m_x = newX;
}
void setY(float newY) {
if (!qFuzzyCompare(newY, m_y))
m_y = newY;
}
private:
float m_x;
float m_y;
float x() const { return m_x; }
float y() const { return m_y; }
};
#endif // PIECE_H
main.cpp
#include "piece.h"
#include <QRangeModel>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QList<piece> mesPieces;
mesPieces.append(piece(20.0f, 20.0f));
mesPieces.append(piece(60.0f, 0.0f));
mesPieces.append(piece(100.0f, 30.0f));
QRangeModel modele(&mesPieces);
QQmlApplicationEngine engine;
engine.setInitialProperties({{"monModele", QVariant::fromValue(&modele) }});
QObject::connect(
&engine,
&QQmlApplicationEngine::objectCreationFailed,
&app,
[]() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
engine.loadFromModule("testQRange1", "Main");
return QGuiApplication::exec();
}
main.qml
import QtQuick
import QtQuick.Controls.Basic
import QtQuick.Shapes
ApplicationWindow {
id: window
width: 640
height: 480
visible: true
title: qsTr("test QRangeModel")
required property AbstractItemModel monModele
Repeater {
model : monModele
Shape {
property real mx : monModele.data(monModele.index(index, 0), Qt.DisplayRole)
property real my : monModele.data(monModele.index(index, 1), Qt.DisplayRole)
anchors.fill: parent
ShapePath {
strokeColor: "yellow"
fillColor: "yellow"
strokeWidth: 1
startX: mx
startY: my
PathLine { relativeX: 0; relativeY: 50 }
PathLine { relativeX: 50; relativeY: 0 }
}
}
}
}
It doesn't work if I try to replace mx and my properties by required property real x (and y)