Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Assigning custom property in dynamically created QML component. How?

Assigning custom property in dynamically created QML component. How?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 4 Posters 1.8k 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.
  • B Offline
    B Offline
    bogong
    wrote on last edited by bogong
    #1

    Hello all!
    I am creating component dynamically from URL and don't able to assign custom property of component.
    CustomComponent.qml

    Rectangle {
    
    	property string pTestText: "Default value";
    		
    	id: idRoot;
    			
    	Text {
    			
    		id: idText;
    		text: idRoot.pTestText;
    	}
    }
    

    Creating component dynamically and the pTestText property do not working, it's getting default value:

    var CustomComponent = Qt.createComponent("qrc:/CustomComponent.qml");
    CustomComponent.pTestText = "Test Text";
    

    When I am doing it directly in QML - everything works fine:

    ...
    CustomComponent {
    	pTestText: "New Value";
    }
    ...
    

    The question is how to define it from JS? Am I missing something?

    1 Reply Last reply
    0
    • IntruderExcluderI Offline
      IntruderExcluderI Offline
      IntruderExcluder
      wrote on last edited by
      #2

      You should set properties to an objects created by this component.

      let component = Qt.createComponent("qrc:/CustomComponent.qml");
      let obj = component.createObject(parent, {/* properties */pTestText: "42"});
      // Properties can be set later
      obj.pTestText = "hello";
      
      B 1 Reply Last reply
      1
      • IntruderExcluderI IntruderExcluder

        You should set properties to an objects created by this component.

        let component = Qt.createComponent("qrc:/CustomComponent.qml");
        let obj = component.createObject(parent, {/* properties */pTestText: "42"});
        // Properties can be set later
        obj.pTestText = "hello";
        
        B Offline
        B Offline
        bogong
        wrote on last edited by bogong
        #3

        @IntruderExcluder But what if I need to push it into StackView? What should be in exchange of parent? My use-case is creating dynamically QML component define parameters and push it into StackView. Will it be destroyed when I pop it out?

        1 Reply Last reply
        -1
        • IntruderExcluderI Offline
          IntruderExcluderI Offline
          IntruderExcluder
          wrote on last edited by
          #4

          You can set parent as null, but you must take care of lifetime of such objects. The parent property is like same as it works with QObject instances: remove all childs on destroy.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bogong
            wrote on last edited by
            #5

            Found solution. This works for me in StackView:

            var CustomComponent = Qt.createQmlObject('
            	import QtQuick 2.0;
            	CustomComponent {}
            ', oStackView, 'oCustomComponent');
            CustomComponent.pTestText = "Value at time of creating";
            oStackView.push(CustomComponent);
            

            Issue closed.

            GrecKoG 1 Reply Last reply
            0
            • IntruderExcluderI Offline
              IntruderExcluderI Offline
              IntruderExcluder
              wrote on last edited by
              #6

              So what's the difference between your example and using push method of StackView?

              J.HilkJ 1 Reply Last reply
              0
              • IntruderExcluderI IntruderExcluder

                So what's the difference between your example and using push method of StackView?

                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @IntruderExcluder probably using the correct property name pTestText instead of the undefined pText


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • B bogong

                  Found solution. This works for me in StackView:

                  var CustomComponent = Qt.createQmlObject('
                  	import QtQuick 2.0;
                  	CustomComponent {}
                  ', oStackView, 'oCustomComponent');
                  CustomComponent.pTestText = "Value at time of creating";
                  oStackView.push(CustomComponent);
                  

                  Issue closed.

                  GrecKoG Offline
                  GrecKoG Offline
                  GrecKo
                  Qt Champions 2018
                  wrote on last edited by
                  #8

                  @bogong said in Assigning custom property in dynamically created QML component. How?:

                  Found solution. This works for me in StackView:

                  var CustomComponent = Qt.createQmlObject('
                  	import QtQuick 2.0;
                  	CustomComponent {}
                  ', oStackView, 'oCustomComponent');
                  CustomComponent.pTestText = "Value at time of creating";
                  oStackView.push(CustomComponent);
                  

                  Issue closed.

                  Or simpler (way simpler) :

                  oStackView.push("CustomComponent.qml", { pTestText: "Value at time of creating" } );
                  
                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    bogong
                    wrote on last edited by
                    #9
                    This post is deleted!
                    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