我正在使用QML开发前端,我在从主QML窗口访问组件时遇到问题 . 所以,我的主要QML窗口是这样的:
ApplicationWindow {
id: rootWindow
objectName: "window"
property Component mainScreen: MainScreen {} // my component
// This is a slot that gets called from C++ side.
// The function gets called fine.
function videoDone() {
mainScreen.doVideo()
}
}
MainScreen
组件在 MainScreen.qml
文件中写为:
ControlView {
id: mainScreenView
objectName: "MainScreenView"
function doVideo() {
console.log("Called")
}
}
但是,这不能按预期工作,我收到错误:
TypeError: Property 'doVideo' of object QQmlComponent is not a function
我认为问题在于,在ApplicationWindow级别没有看到 MainScreen
的完整定义 . 我试着看看我是否可以施展它但没有成功 .
此外 mainScreen.objectName
返回空字符串而不是 MainScreenView
4 回答
您必须导入组件,组件名称和文件名必须相同 .
MainScreen.qml
我认为使用
MainScreen
组件的正确方法是这样做:main.qml
MainScreen.qml (与您相同的代码,但我使用了
Item
而不是ControlView
来检查编译)另一种选择是创建组件dynamically .
代替
使用
这是示例代码
main.qml
MainScreen.qml
我猜你只想要这样的东西:
并在MainScreen.qml中: