Qml 和外部js文件协同工作

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

               

可以用import as语句将外部js文件引入到qml文件中,下面定义了一个qml文件,里面只有一个Item.

import QtQuick 2.0import "handler.js" as HandlerItem {    id: top_item    width: 380    height: 200    MouseArea {         anchors.fill: parent        onPressed: Handler.onPressed(mouse);    }}

看一下handler.js文件:

function onPressed(mouse) {    console.log(mouse.x, mouse.y);}

当鼠标单击的时候,控制台将打印光标的位置。


再复杂一点,让qml的Item在另一个Rectangle里面, 然后转换坐标位置:

import QtQuick 2.0import "handler.js" as HandlerRectangle {    id: r1    width: 400    height: 400    Item {        id: i1        anchors.fill: parent.fill        Rectangle {            id: r2            width: 200            height: 200            color: "red"            anchors.left: parent.left            anchors.leftMargin: 100            anchors.top: parent.top            anchors.topMargin: 100            MouseArea {                anchors.fill: parent                onPressed: Handler.onPressed(mouse);            }        }    }}

handler.js文件:

function onPressed(mouse) {    console.log("postion in r2 Rectangle");    console.log(mouse.x, mouse.y);    console.log("postion in r1 Rectangle");    var pos = r1.mapFromItem(r2, mouse.x, mouse.y);    console.log(pos.x, pos.y);}

运行一下:

 ~/Qt5.2.0/5.2.0/gcc_64/bin/qmlscene ./test1.qml


鼠标点击红色区域后,控制台输出:

postion in r2 Rectangle114 119postion in r1 Rectangle214 219

r1.mapFromItem(r2, mouse.x, mouse.y) 意思是将

r2坐标系的鼠标位置转换成r1坐标系的位置。


还有mapToItem,刚好相反,是将r1坐标系下的鼠标位置转换到r2的坐标系下,注意不要看反了。

http://doc-snapshot.qt-project.org/qdoc/qml-qtquick-item.html#mapToItem-method-2


           

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QML界面中显示外部程序,可以通过将外部程序加载到QWidget框架中,然后再将QWidget窗口加载到QML界面中实现。具体步骤如下: 1. 创建一个QWidget窗口,并将外部程序加载到该窗口中。 2. 将QWidget窗口加载到QML界面中。 下面是一个示例代码: ```cpp // main.cpp #include <QApplication> #include <QQmlApplicationEngine> #include <QQuickWidget> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建一个QWidget窗口 QQuickWidget widget; widget.setSource(QUrl("qrc:/qml/ExternalProgram.qml")); // 加载外部程序的QML文件 // 创建一个QQmlApplicationEngine QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/qml/Main.qml"))); // 加载主界面的QML文件 // 将QWidget窗口加载到QML界面中 QObject *rootObject = engine.rootObjects().first(); QQuickItem *qmlItem = qobject_cast<QQuickItem*>(rootObject); qmlItem->setProperty("externalProgram", QVariant::fromValue(widget.rootObject())); return app.exec(); } ``` ```qml // Main.qml import QtQuick 2.0 Item { width: 800 height: 600 // 外部程序的容器 Item { id: externalProgramContainer anchors.fill: parent } // 加载外部程序的QML文件 Component.onCompleted: { externalProgramContainer.children = [externalProgram]; } } ``` ```qml // ExternalProgram.qml import QtQuick 2.0 Rectangle { width: 400 height: 300 color: "lightblue" // 外部程序的内容 Text { text: "External Program" anchors.centerIn: parent font.pixelSize: 24 } } ``` 通过以上代码,我们创建了一个QWidget窗口,并将外部程序的QML文件加载到该窗口中。然后,我们将该QWidget窗口加载到主界面的QML文件中的一个容器中,从而在QML界面中显示外部程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值