尝试访问qml全局变量,但报告错误

时间:2019-01-25 11:52:47

标签: qt qml qtquick2

我的代码

AppDelegate.js 用于全局变量

.pragma library

var appDelegate= {}

MyApp.js

Qt.include("AppDelegate.js")



function AppDelegate(){

}


AppDelegate.prototype.doSomething=function() {


    console.log("vvvvvvvvvvvvv")

}
AppDelegate.prototype.doSomething1=function() {


    console.log("AAAAAAAAAAAAAA")

}
//-------------------------------------------------------
function initApp(){


    appDelegate=new AppDelegate()
    appDelegate.doSomething()
    appDelegate.doSomething1()

}

MyApp.qml

import QtQuick 2.2
import QtQuick.Controls 1.2
import "MyApp.js" as MyApp


ApplicationWindow {
    visible: true
    title: "System Dialogs Gallery"
    width: 580
    height: 480

    TabView {
        anchors.fill: parent
        anchors.margins: 8
        Tab {
            id: controlPage
            title: "File"
            FileDialogs { }
        }

    }

    Component.onCompleted: {
        MyApp.initApp()

    }
}

效果很好,输出是

qml: vvvvvvvvvvvvv
qml: AAAAAAAAAAAAAA

但是如果我从其他.js .qml文件调用

ColorDialogs.js

Qt.include("AppDelegate.js")



function doit(){



    appDelegate.doSomething1()

}

ColorDialogs.qml

import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Dialogs 1.1

import "ColorDialogs.js" as ColorDialogs



Item {


    Component.onCompleted: {


        ColorDialogs.doit()

    }
}

它报告错误:

ColorDialogs.js: : TypeError: Property 'doSomething1' of object [object Object] is not a function

欢迎评论

----------------------------更新

enter image description here

0 个答案:

没有答案