[英]QML accessing your qml component
I am using QML for developing a front end and I have an issue accessing a component from my main QML Window. 我正在使用QML开发前端,但是从主QML窗口访问组件时遇到问题。 So, my main QML window is something as:
因此,我的主要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()
}
}
The MainScreen
component is written in MainScreen.qml
file as: MainScreen
组件写在MainScreen.qml
文件中为:
ControlView {
id: mainScreenView
objectName: "MainScreenView"
function doVideo() {
console.log("Called")
}
}
However, this does not work as expected and I get the error: 但是,这不能按预期方式工作,并且出现错误:
TypeError: Property 'doVideo' of object QQmlComponent is not a function
I think the issue is that the full definition of MainScreen
is not being seen at the ApplicationWindow level. 我认为问题在于在ApplicationWindow级别上看不到
MainScreen
的完整定义。 I tried to see if I can cast it but no success. 我试图看看是否可以投放,但没有成功。
Also mainScreen.objectName
returns a null string rather than MainScreenView
同样
mainScreen.objectName
返回一个空字符串,而不是MainScreenView
I think the right way to use the MainScreen
component is doing something like this: 我认为使用
MainScreen
组件的正确方法是执行以下操作:
main.qml main.qml
import QtQuick 2.5
import QtQuick.Controls 1.4
ApplicationWindow {
id: rootWindow
objectName: "window"
visible: true
MainScreen { id: mainScreen } // my component
Component.onCompleted: {
mainScreen.doVideo()
}
}
MainScreen.qml (same code than you, but I've used Item
instead of ControlView
just to check the compilation) MainScreen.qml (与您的代码相同,但是我使用
Item
代替ControlView
只是为了检查编译情况)
import QtQuick 2.5
Item {
id: mainScreenView
objectName: "MainScreenView"
function doVideo() {
console.log("Called")
}
}
Another option would be to create the component dynamically . 另一种选择是动态创建组件。
Instead of 代替
Property Component mainscreen: MainScreen { }
属性组件主屏幕:MainScreen {}
Use 采用
Property var mainscreen: MainScreen { }
属性var mainscreen:MainScreen {}
Here is sample code 这是示例代码
main.qml main.qml
import QtQuick 2.6 import QtQuick.Controls 1.5 ApplicationWindow { id: rootWindow objectName: "window" visible: true property var mainScreen: MainScreen { } Component.onCompleted: { mainScreen.doVideo() } }
MainScreen.qml MainScreen.qml
import QtQuick 2.6 Item { id: mainScreenView objectName: "MainScreenView" function doVideo() { console.log("Called") } }
I guess you just want something like this: 我猜你只想要这样的东西:
ApplicationWindow {
// if you really need a property of your Item uncomment the following line:
// property alias mainScreen : mainScreen
MainScreen {
id: mainScreen
}
function videoDone() {
mainScreen.doSomething()
}
}
and in the MainScreen.qml: 并在MainScreen.qml中:
import QtQuick 2.0
Item {
function doSomething()
{
console.debug("Test")
}
}
You should assign id to your component. 您应该为组件分配ID。
ApplicationWindow { ApplicationWindow {
id: rootWindow
objectName: "window"
property Component mainScreen: MainScreen {
id: myMainScreenCmp
} // my component
function videoDone() {
myMainScreenCmp.doVideo()
}
} }
You have to import your component and your component name and filename must be the same. 您必须导入组件,并且组件名称和文件名必须相同。
MainScreen.qml MainScreen.qml
MainScreen {
id: mainScreenView
objectName: "MainScreenView"
function doVideo() {
console.log("Called")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.