在QML中将多个矩形绘制到图像中

时间:2018-07-12 08:14:21

标签: qt qml qt5

我正在尝试将矩形绘制到QML中。包含有关这些矩形的信息的数据如下所示:

  • X
  • Y
  • 宽度
  • 高度
  • 数据

数据存储在数组中,数组中的每个项目代表一个矩形。我正在寻找绘制这些矩形的最佳方法(或者至少是一种好的方法)。

我应该使用哪个QML组件?

class.h

class MyClass : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QList<structure> list READ list NOTIFY listChanged)
    public:
        QList<structure> list() const { return list_; }
    signals:
        listChanged();
    private:
        QList<structure> list_;
}

repeater.qml

Repeater {
    model: 2 
    delegate: Rectangle{
        width: model.list.width
        height: model.list.height
        x: model.list.x
        y:  model.list.y
        color: "red"
    }
}

1 个答案:

答案 0 :(得分:4)

不必创建QObject,仅存储QVariantList的{​​{1}}就足够了。另一方面,您必须传递的模型只是QRect的列表,要访问委托中的每个QRect,您必须使用QRect

main.cpp

modelData

main.qml

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include <QRect>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QVariantList rectlist;
    rectlist<< QRect{50, 30, 100, 100}
            << QRect{200, 20, 30, 30}
            <<QRect{300, 300, 200, 33}
           <<QRect{400, 23, 44, 55};

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("rectlist", rectlist);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

enter image description here


更新

main.cpp

import QtQuick 2.9
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Repeater {
        model: rectlist
        delegate: Rectangle{
            x: modelData.x
            y: modelData.y
            width: modelData.width
            height: modelData.height
            color: "red"
        }
    }    
}

main.qml

#include <QColor>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include <QRect>

struct Data
{
    Q_GADGET
    Q_PROPERTY(QRect rect MEMBER rect)
    Q_PROPERTY(QString text MEMBER text)
    Q_PROPERTY(QColor color MEMBER color)
public:
    QRect rect;
    QString text;
    QColor color;
    Data(const QRect& rect= QRect(), const QString& text="", const QColor& color = QColor(Qt::transparent)){
        this->rect = rect;
        this->text = text;
        this->color = color;
    }
};
Q_DECLARE_METATYPE(Data)

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);
    QVariantList rectlist;
    rectlist <<QVariant::fromValue( Data{ QRect{50, 30, 100, 100}, "text1", Qt::red});
    rectlist <<QVariant::fromValue( Data{ QRect{200, 20, 30, 30 }, "text2", QColor("blue")});
    rectlist <<QVariant::fromValue( Data{ QRect{300, 300, 200,33}, "text3", QColor(0, 200, 0)});
    rectlist <<QVariant::fromValue( Data{ QRect{400, 23, 44, 55 }, "text4"});

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("rectlist", rectlist);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

#include "main.moc"

enter image description here