I am using QML for developing a front end and I have an issue accessing a component from my main QML Window. So, my main QML window is something as:
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:
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. I tried to see if I can cast it but no success.
Also mainScreen.objectName returns a null string rather than MainScreenView
I think the right way to use the MainScreen component is doing something like this:
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)
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 { }
Use
Property var mainscreen: MainScreen { }
Here is sample code
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
import QtQuick 2.6
Item {
id: mainScreenView
objectName: "MainScreenView"
function doVideo() {
console.log("Called")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With