QML动态组件创建

发布于 2024-12-21 01:50:22 字数 1612 浏览 3 评论 0原文

我正在尝试通过 Javascript 函数以编程方式在 QML 中创建 QtMobility MapPolyline 的实例和几个 坐标

据我所知,从 Javascript 函数创建新实例的唯一方法是使用 Qt.createComponent 和 Qt.createQmlObject 。但是,我找不到有效的调用 createComponent 的方法(我总是收到文件不存在错误)。我想避免 Qt.createQmlObject ,因为这似乎是一个非常糟糕的做法。

有没有什么干净的方法来实现这一目标?

Component {
    id: polyGenerator
    MapPolyline {}
}

function addPoly() {
    //This next line works, but crashes when trying to add positions
    //createPoly(polyGenerator);

    var component = Qt.createComponent("Rectangle");
    console.log(component.status + " " + Component.Null);
    if (component.status == Component.Ready) {
        createPoly(component);
    } else if (component.status == Component.Error) {
        console.log("Error: " + component.errorString());
    } else {
        component.statusChanged.connect(function () {
            if (component.status == Component.Error) {
                console.log("Error: " + component.errorString());
                return;
            }
            createPoly(component);
        });
    }
}

function createPoly(component) {
    var poly = component.createObject(map);

    poly.border.color = "red";
    poly.border.width = 4;

    // I get a crash here, my guess is that I need proper Coordinate objects
    poly.addCoordinate({latitude: -34.60553, longitude: -58.38088});
    poly.addCoordinate({latitude: -34.60720, longitude: -58.38081});
    poly.addCoordinate({latitude: 34.60720, longitude: -58.38081});
    poly.addCoordinate({latitude: -34.60597, longitude: -58.37930});

    map.addMapObject(poly);
}

I'm trying to create an instance of a QtMobility MapPolyline and several of Coordinate in QML programmaticaly from a Javascript function.

As far as I can see the only way to create new instance from Javascript functions is to use Qt.createComponent and Qt.createQmlObject. However, I can't find a way to call createComponent that will work (I always get a file doesnt exist error). And I'd like to avoid Qt.createQmlObject since it seems like a really bad practice.

Is there any clean way to achieve this?

Component {
    id: polyGenerator
    MapPolyline {}
}

function addPoly() {
    //This next line works, but crashes when trying to add positions
    //createPoly(polyGenerator);

    var component = Qt.createComponent("Rectangle");
    console.log(component.status + " " + Component.Null);
    if (component.status == Component.Ready) {
        createPoly(component);
    } else if (component.status == Component.Error) {
        console.log("Error: " + component.errorString());
    } else {
        component.statusChanged.connect(function () {
            if (component.status == Component.Error) {
                console.log("Error: " + component.errorString());
                return;
            }
            createPoly(component);
        });
    }
}

function createPoly(component) {
    var poly = component.createObject(map);

    poly.border.color = "red";
    poly.border.width = 4;

    // I get a crash here, my guess is that I need proper Coordinate objects
    poly.addCoordinate({latitude: -34.60553, longitude: -58.38088});
    poly.addCoordinate({latitude: -34.60720, longitude: -58.38081});
    poly.addCoordinate({latitude: 34.60720, longitude: -58.38081});
    poly.addCoordinate({latitude: -34.60597, longitude: -58.37930});

    map.addMapObject(poly);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反目相谮 2024-12-28 01:50:22

Qt.createComponent 需要一个 URL 来加载文件。这就是为什么它会抱怨文件丢失;您正在尝试加载一个名为“矩形”的文件。

如果您只想动态创建对象,请创建组件声明式。

Component {
    id: component
    Rectangle {
    }
}

这样 createPoly 函数应该可以正常工作。

Qt.createComponent needs a URL to load a file from. That's why it complains about a missing file; you're trying to load a file called "Rectangle".

If you just want to create objects dynamically create your component declarative.

Component {
    id: component
    Rectangle {
    }
}

That way the createPoly function should work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文