我想在 QML 中绘制许多矩形.Rectangulars (x,y) 的坐标将在 C++ 中计算,并且需要传递给 QML.
I want to draw many Rectangular's inside a QML. The coordinates for Rectangulars (x,y) will be calculated inside C++, and need to be passed to QML.
我试图寻找 ListView 或 Column 或 Row 元素(使用中继器),但这些似乎都不能解决问题,因为元素的排列方式类似于表格"......我面临缺乏有关此类案例的网络示例.
I tried to look for ListView or Column or Row elements (with Repeater), but non of this seems to solve the problem, because the elements are arranged like in 'tables'... I faced the lack of information on the web examples on such case.
更具体地说:1. 如何将矩形的多个坐标传递给 QML?(什么数据结构)2. 我应该使用什么机制来显示它们?
To be more specific: 1. How to pass many coordinates for my Rectangular's to QML? (what data structure) 2. What mechanism should I use to display them?
推荐答案您可以简单地:
import QtQuick 2.0 Rectangle { id: root height: 500 width: 500 property string sc: 'import QtQuick 2.0; Rectangle {width: 20; height: 20; color: "red"; Component.onCompleted: {x = Math.random() * parent.width; y = Math.random() * parent.height;}}' Component.onCompleted: { for (var i = 0; i < 10; ++i) Qt.createQmlObject(sc, root, 'obj' + i); } }此代码将创建指定数量的矩形并将它们随机放置在父矩形上.
This code will create the specified number of rectangles and position them randomly over the parent rectangle.
如果需要在 C++ 中生成位置,则必须创建自定义 C++ 元素,这是一个基本示例:
If generating the position in C++ is a necessity, you will have to create a custom C++ element, here is a basic example:
class Positioner : public QObject { Q_OBJECT public: explicit Positioner(QObject *parent = 0) : QObject(parent) {} public slots: QPointF getPosition() { return QPoint(qrand() % 500, qrand() % 500); } };在 main.cpp 中注册:
Register it in main.cpp:
qmlRegisterType<Positioner>("TestComponent", 1, 0, "Positioner");最后使用它,这次我们使用Positioner的getPosition() C++槽:
And finally use it, this time we use the Positioner's getPosition() C++ slot:
import QtQuick 2.0 import TestComponent 1.0 Rectangle { id: root height: 500 width: 500 Positioner {id: myPositioner} property string sc: 'import QtQuick 2.0; Rectangle {width: 20; height: 20; color: "red"; Component.onCompleted: {var pos = myPositioner.getPosition(); x = pos.x; y = pos.y;}}' Component.onCompleted: { for (var i = 0; i < 10; ++i) Qt.createQmlObject(sc, root, 'obj' + i); } }您还可以使用现有的 QML 文件代替字符串,这样更方便一些,因为您可以自动完成.那将是您的 TestItem 组件,只是为了好玩,它的实例会在单击时自行删除:
You can also use an existing QML file instead of a string, which is a little more convenient, since you get autocomplete. That would be your TestItem component and just for fun, its instances will delete themselves when clicked:
import QtQuick 2.0 Rectangle { width: 100 height: 100 color: "black" MouseArea { anchors.fill: parent onClicked: parent.destroy() } Component.onCompleted: { var pos = myPositioner.getPosition() x = pos.x y = pos.y } }在你的主 QML 文件中实例化它:
And in your main QML file you instantiate it:
var component = Qt.createComponent("TestItem.qml") component.createObject(root)如果您不想在 QML 中实例化定位器,而是在 QML 中实例化和使用单个对象,您可以执行以下操作(在 main.cpp 中):
In case you don't want to instantiate a Positioner in QML and instead instantiate and use a single object in QML, you can do the following (in main.cpp):
Positioner p; view.rootContext()->setContextProperty("sPositioner", &p);并在 QML 中使用:
And use in QML:
var pos = sPositioner.getPosition()更多推荐
QML 对象的动态实例化?
发布评论