Dynamic component makes multiple components !!! in qml
-
hi, im using pyqt6 and i have a problem.
i used below code to make a component dynamicallyproperty var dtaComp: Qt.createComponent("Data.qml") property var dtaObj: dtaComp.createObject(crt_MainFrame, { x: (crt_frmLeft.width < 250) ? 59 : 259, width: (crt_frmLeft.width < 250) ? 993 : 743, y: 109, height: crt_MainFrame.height - 110, } )
and in Data.qml :
Component.onCompleted: { print("******* CREATED *******") }
when i run this i got this message for 4 time:
'' ******* CREATED ******* ''
i don't know why it is happenedi just wanna make Data class and show this dynamically
i don't really know what is wrong i'm confused
thanks for any help- i commented this code and there is no any CREATED message anymore. so we know problem is in that code
solution:
in last god helped me (:
i understood that when crt_frmLeft.width or height changed my object class also will be make again ( i don't know exactly ) so becarefull to don't bind this object propertiescan tell me anybody why that is like this? this is sooooo bad ):
-
Why are you using dynamic creation here?
Wouldn't the following do what you need?property Data dtaObj: Data { x: (crt_frmLeft.width < 250) ? 59 : 259 width: (crt_frmLeft.width < 250) ? 993 : 743 y: 109 height: crt_MainFrame.height - 110 }
As for the original question, it's just how QML and property bindings work. If you are accessing a property in a declarative binding and this property changes, the binding is reevaluated. Here it means calling
dtaComp.createObject
again. That's what makes QML so good, but you have to think declaratively to make the most use out of it. -
Why are you using dynamic creation here?
Wouldn't the following do what you need?property Data dtaObj: Data { x: (crt_frmLeft.width < 250) ? 59 : 259 width: (crt_frmLeft.width < 250) ? 993 : 743 y: 109 height: crt_MainFrame.height - 110 }
As for the original question, it's just how QML and property bindings work. If you are accessing a property in a declarative binding and this property changes, the binding is reevaluated. Here it means calling
dtaComp.createObject
again. That's what makes QML so good, but you have to think declaratively to make the most use out of it.