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. Why can't only the customed property "kind" one be allocated data correctly?
Forum Updated to NodeBB v4.3 + New Features

Why can't only the customed property "kind" one be allocated data correctly?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 430 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.
  • M Offline
    M Offline
    melliol
    wrote on last edited by
    #1

    Below are the part of my main codes where I try to allocate the data"root.bulletkind" which has been defined correctly to a incubator to creat a object.
    屏幕截图 2024-12-20 115907.png
    What the problem is that except"kind",all of the other property could be allocated correctly,and toward is a customed property,and below is the part of my bullet.qml codes:
    屏幕截图 2024-12-20 115602.png
    It always console log "undefined" for "root.kind",I don't know the reason.I think the customed proeprety "toward" is same to it,but it just can't work correctly😵‍💫

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by dheerendra
      #2

      Ids don't work like properties. You can try something like this.

      function fire(t,nx,ny){
              var comp = Qt.createComponent("MyBullet.qml");
              var incubateMe = comp.incubateObject(root,{x:nx,y:ny,toward:t})
              incubateMe.onStatusChanged = function(status){
                  if (status === Component.Ready){
                      console.log("Bullet object is ready ")
                      incubateMe.object.kind = root.bulletKind
                      incubateMe.object.visible = true
                      incubateMe.object.color = "red";
                  }
              }
          }
      

      You can refer the complete example here

      Also don't post the images. pasting the code or example will help.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      M 1 Reply Last reply
      0
      • M Offline
        M Offline
        melliol
        wrote on last edited by
        #3

        Below are the code of the main QML I have corrected

        function fire(t,nx,ny)
        {
            var com=Qt.createComponent("bullet.qml");
            var incubator=com.incubateObject(root,{x:nx,y:ny,toward:t});
            incubator.onStatusChanged=function(status)
            {
                if(status===Component.Ready)
                {
                    console.log("OK");
                    incubator.object.visible=true;
                    incubator.object.kind=root.bulletkind
                    incubator.object.destroy(4000);
        
                    addbullet(incubator.object);
                }
            }
        }
        

        And below are my bullet.qml:

        import QtQuick
        
        Rectangle
        {
            id:root;
            width:20;
            height:width;
            radius:width/2;
            visible:false;
            z:1;
            color:"yellow";
            property var toward;//1对应左,2对应上,3对映右,4对应下
            property int speed:200;
            property var kind;
            // property var bound:[];//1对应左,2对应上,3对映右,4对应下
        
            onVisibleChanged:
            {
                if(visible)
                {
                    console.log(root.kind);
                    moveanimation(root.toward,root.kind);
                }
            }
            // onXChanged:
            // {
            //     if(x<=bound[1]||x>=bound[3])
            //     {visible=false;
            //     console.log("消失在x")}
            // }
            // onYChanged:
            // {
            //     if(y<=bound[2]||y>=bound[4])
            //      {   visible=false;
            //     console.log("xiao shi zai y")}
            // }
            NumberAnimation
            {
                id:ani;
                target:root;
                easing.type:Easing.Linear;
                onFinished:
                {
                    root.visible=false;
                }
            }
        
            function moveanimation(a,b)//a用来选择方向,b用来选择动画
            {
                //here I want to use kind to choose the style that the bullet shoot
                // switch(b)
                // {
                // case 1:
                // {
                //     ani.easing.type=Easing.InOutExpo;
                //     root.speed=200;
                //     break;
                // }
                // case 2:
                // {
                //     ani.easing.type=Easing.Linear;
                //     root.speed=300;
                //     break;
                // }
                // case 3:
                // {
                //     ani.easing.type=Easing.InOutElastic;
                //     root.speed=200;
                //     break;
                // }
                // }
                switch(a)
                {
                case 1:
                {
                    ani.property="x";
                    ani.duration=1000*x/(root.speed);
                    ani.to=0;
                    ani.start();
                    break;
                }
                case 2:
                {
                    ani.property="y";
                    ani.to=0;
                    ani.duration=1000*y/(root.speed);
                    ani.start();
                    break;
                }
                case 3:
                {
        
                    ani.property="x";
                    ani.to=640;
                    ani.duration=1000*(640-x)/(root.speed);
                    ani.start();
                    break;
                }
                case 4:
                {
        
                    ani.property="y";
                    ani.to=480;
                    ani.duration=1000*(480-y)/(root.speed);
                    ani.start();
                    break;
                }
                }
            }
        }
        
        1 Reply Last reply
        0
        • dheerendraD dheerendra

          Ids don't work like properties. You can try something like this.

          function fire(t,nx,ny){
                  var comp = Qt.createComponent("MyBullet.qml");
                  var incubateMe = comp.incubateObject(root,{x:nx,y:ny,toward:t})
                  incubateMe.onStatusChanged = function(status){
                      if (status === Component.Ready){
                          console.log("Bullet object is ready ")
                          incubateMe.object.kind = root.bulletKind
                          incubateMe.object.visible = true
                          incubateMe.object.color = "red";
                      }
                  }
              }
          

          You can refer the complete example here

          Also don't post the images. pasting the code or example will help.

          M Offline
          M Offline
          melliol
          wrote on last edited by
          #4

          @dheerendra Sorry,I have tried like you telling,but it still can't work in my project😵‍💫I am ready confused why could this happen?

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

            Code is not exactly same as mine. Revert the order like the following. It works.

            incubator.object.kind=root.bulletkind
            incubator.object.visible=true;

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            M 1 Reply Last reply
            1
            • dheerendraD dheerendra

              Code is not exactly same as mine. Revert the order like the following. It works.

              incubator.object.kind=root.bulletkind
              incubator.object.visible=true;

              M Offline
              M Offline
              melliol
              wrote on last edited by
              #6

              @dheerendra Amazing🥳!It works,but why could not it run correctly before?I think thery are similar,just a little different.Is the problem on the order of the property?🧐

              1 Reply Last reply
              0
              • M melliol has marked this topic as solved on
              • M melliol has marked this topic as solved on

              • Login

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