Qt How to display a list of C++ QQuickPaintedItem in QML

Viewed 46

I have a list of C++ objects derived from QQuickPaintedItem. Each object is capable of painting itself. How do I display this list of objects in QML? I wanted to use Repeater, but if I set the model of the Repeater to the list, what do I put as delegate since the objects can paint themselves?

RandomObject.h:

class RandomObject
{
    Q_OBJECT
    Q_PROPERTY(QList<Paintable*> paintable_list READ paintableList NOTIFY paintableListChanged)
public:
    RandomObject(QObject* parent=nullptr);
    QList<Paintable*> paintableList();
signals:
    void paintableListChanged();
}

paintable.h:

class Paintable : public QQuickPaintedItem
{
    Q_OBJECT
public:
    Paintable(QQuickPaintedItem* parent=nullptr);
protected:
    paint(QPainter* painter);
}

main.cpp:

int main(int argc, char *argv[])
{
    if (qEnvironmentVariableIsEmpty("QTGLESSTREAM_DISPLAY")) {
        qputenv("QT_QPA_EGLFS_PHYSICAL_WIDTH", QByteArray("213"));
        qputenv("QT_QPA_EGLFS_PHYSICAL_HEIGHT", QByteArray("120"));

        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    }

    QGuiApplication app(argc, argv);
    app.setAttribute(Qt::AA_UseHighDpiPixmaps);
    RandomObject random;
    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("RandomObject", &random);
    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();
}

main.qml:

Window {
    width: 640
    height: 600
    visible: true
    title: qsTr("Hello World")
    Repeater
    {
        model: RandomObject.paintable_list
        delegate: //what to specify here?
    }
}
0 Answers
Related