Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Assigning Qt Item to Property



  • Hello Qt Friends :),

    I have created following item named BasicStyle.qml:

    import QtQuick 2.0
    
    Item {
    	property color backgroundColor: "red"
    }
    

    and item OtherStyle.qml that 'derives' from it:

    import QtQuick 2.0
    
    BasicStyle {
    	backgroundColor: "green"
    }
    

    Now I would like to use like this:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    
    Window {
    	visible: true
    	width: 640
    	height: 480
    	title: qsTr("Hello World")
    
    //	property BasicStyle basicStyle: BasicStyle
    	property BasicStyle basicStyle: OtherStyle
    
    	color: basicStyle.backgroundColor
    }
    

    However all I can get is:

    QML debugging is enabled. Only use this in a safe environment.
    qrc:/main.qml:13: TypeError: Cannot read property 'backgroundColor' of null
    qrc:/main.qml:11:2: Unable to assign QQuickWindowQmlImpl_QML_1 to BasicStyle_QMLTYPE_0
    

    Do you have any idea what I'm missing here?



  • @poor_robert hi
    please see Style Singleton here https://wiki.qt.io/Qml_Styling

    @poor_robert said in Assigning Qt Item to Property:

    Do you have any idea what I'm missing here?

    i think you need brackets after OtherStyle to make it work

    property BasicStyle basicStyle: OtherStyle{}
    


  • @LeLev Thank you sir :-). I already have seen the styling singleton. However I didn't want to use it.

    Thanks for pointing missing: {}. Adding brackets solved the problem.


Log in to reply