Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Assigning Qt Item to Property

Assigning Qt Item to Property

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 305 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    poor_robert
    wrote on last edited by
    #1

    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?

    ODБOïO 1 Reply Last reply
    0
    • P poor_robert

      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?

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      @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{}
      
      P 1 Reply Last reply
      0
      • ODБOïO ODБOï

        @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{}
        
        P Offline
        P Offline
        poor_robert
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved