Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML accessing your qml component

Tags:

qt

qml

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

like image 483
Luca Avatar asked Oct 29 '25 08:10

Luca


2 Answers

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.

like image 137
Tarod Avatar answered Oct 31 '25 10:10

Tarod


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")
    }
}
like image 44
Rajesh Reddy Avatar answered Oct 31 '25 09:10

Rajesh Reddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!