如何将 Component.onCompleted:{} 方法从 ui.qml 转移到 .qml? [英] how can I transfer Component.onCompleted:{} method from ui.qml to .qml?

查看:11
本文介绍了如何将 Component.onCompleted:{} 方法从 ui.qml 转移到 .qml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过分离 ui.qml 文件和 .qml 文件来开发一个项目.因此,我将功能代码(javascript 代码)写入 .qml 文件,并在 ui.qml 文件中编写设计代码.但是,我在 .qml 文件中使用 Component.onComplete 功能时遇到问题.

I am developing a project by seperating ui.qml files and .qml files . So, I am writing the functionality codes(javascript codes) into .qml file and I am writing design codes in ui.qml file . However I have a problem with using Component.onComplete functionality in .qml file.

例如:

MapDisplayForm.ui.qml

Item{
   id:item1

   property alias map1

   Map{
       id: map1
       Component.OnCompleted : {
          //this is the function that i should write in Map.qml
       }
   }

}

<小时>

MapDisplay.qml

MapDisplayForm{


   //it does not accept map1.oncompleted here


}

推荐答案

您可以使用 QML Connections 项解决此问题.作为我的 form1.qml 中的一个例子,我有

You can solve this problem using the QML Connections item. As an example in my form1.qml I have

SlideToAction {
    id: sosSlider
    height: 80
    Component.onCompleted: {
        if (window.getHomeLocation()) {
            normalBMargin = 0
        } else {
            normalBMargin = -80
        }
    }
}

将其放入 form1Form.ui.qml 和 form1.qml 格式:form1Form.ui.qml:

To put this into the form1Form.ui.qml and form1.qml format: form1Form.ui.qml:

property alias sosSlider:sosSlider
SlideToAction {
    id: sosSlider
    height: 80
}

属性别名 sosSlider:sosSlider 就像是说 这个属性是公共的,否则将不起作用.

The property alias sosSlider:sosSlider is like saying this property is public and won't work otherwise.

form1.qml:

Connections {
    target: sosSlider
    Component.onCompleted {
        // Your code here
    }
}

这解决了我的问题.

我喜欢拆分 UI 和功能的想法.它让人想起 Ionic/Angular 以及 Django 视图使用的 MVC 样式.它使玩 UI 变得非常容易,而不必担心依赖关系或其他 qml 限制.

I like the idea of splitting up the UI and functionality. It reminds of the MVC style that Ionic/Angular and also Django views use. It makes it really easy to play around with the UI without worrying about dependencies or other qml restrictions.

希望这会有所帮助!

这篇关于如何将 Component.onCompleted:{} 方法从 ui.qml 转移到 .qml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆