Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. why the varrible that i define in file.js it works in MouseArea but it don't works in Text or Image Type?
Qt 6.11 is out! See what's new in the release blog

why the varrible that i define in file.js it works in MouseArea but it don't works in Text or Image Type?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
15 Posts 2 Posters 4.2k Views 2 Watching
  • 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.
  • stackprogramerS stackprogramer

    @crazymax said:

    @stackprogramer
    this works fine for me. Qt 5.5.1
    file.js

    var foo = "some text"
    

    main.qml

    import QtQuick 2.3
    import QtQuick.Controls 1.2
    import QtQuick.Layouts 1.1
    
    import "file.js" as Coreapp
    
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        id: root
        Text{id:newImage;
        text:Coreapp.foo;
    
        }
    
    }
    
    
    

    you are right but in this my source that here it dont work,i want to it work it type image.

     Image {id:imagequestion
                    property string numberFolder:Math.ceil((Math.random()*4))
                     // property string addressImage:4;
                      ParamGenSpiral.flagStart: 1;
                     source:"Image-Core-Render/SVG-Fruit"+numberFolder+"th/Fruit"+numberFolder+"-"+Math.ceil(Math.random()*9)+".svg";
                    width:100
                    height:100
    
                    anchors.centerIn: parent
    
                    RotationAnimation on rotation {
                        duration:600
                        loops: Animation.Infinite
                        from: 0
                        to:360
                      }
    
                }
    

    ParamGenSpiral.Flag is a varible that i defined in js,but it don't work in image type but it work in mousearea

    C Offline
    C Offline
    crazymax
    wrote on last edited by
    #6

    @stackprogramer said:

    ParamGenSpiral.flagStart:

    as I see you try to set flagStart property of ParamGenSpiral alias in Image , but Image don't have such member.
    in mouseArea you use set text in mouseArea method, so it are 2 different cases.

    onclicked:{text:coreapp.foo;}
    }
    

    What do you want to do with ParamGenSpiral.flagStart in Image?

    stackprogramerS 1 Reply Last reply
    0
    • C crazymax

      @stackprogramer said:

      ParamGenSpiral.flagStart:

      as I see you try to set flagStart property of ParamGenSpiral alias in Image , but Image don't have such member.
      in mouseArea you use set text in mouseArea method, so it are 2 different cases.

      onclicked:{text:coreapp.foo;}
      }
      

      What do you want to do with ParamGenSpiral.flagStart in Image?

      stackprogramerS Offline
      stackprogramerS Offline
      stackprogramer
      wrote on last edited by
      #7

      @crazymax said:

      @stackprogramer said:

      ParamGenSpiral.flagStart:

      as I see you try to set flagStart property of ParamGenSpiral alias in Image , but Image don't have such member.
      in mouseArea you use set text in mouseArea method, so it are 2 different cases.

      onclicked:{text:coreapp.foo;}
      }
      

      What do you want to do with ParamGenSpiral.flagStart in Image?

      i want to ParamGenSpiral.flagStart use that save qurl a image,i in the past i save Qurl location image in a js varrible ParamGenSpiral.flagStart now i use it to source my image
      Image{

      addresssimage:ParamGenSpiral.flagStart;
      source:addresssimage;
            }  
      

      but it can not detected in type image!!

      C 1 Reply Last reply
      0
      • stackprogramerS stackprogramer

        @crazymax said:

        @stackprogramer said:

        ParamGenSpiral.flagStart:

        as I see you try to set flagStart property of ParamGenSpiral alias in Image , but Image don't have such member.
        in mouseArea you use set text in mouseArea method, so it are 2 different cases.

        onclicked:{text:coreapp.foo;}
        }
        

        What do you want to do with ParamGenSpiral.flagStart in Image?

        i want to ParamGenSpiral.flagStart use that save qurl a image,i in the past i save Qurl location image in a js varrible ParamGenSpiral.flagStart now i use it to source my image
        Image{

        addresssimage:ParamGenSpiral.flagStart;
        source:addresssimage;
              }  
        

        but it can not detected in type image!!

        C Offline
        C Offline
        crazymax
        wrote on last edited by crazymax
        #8

        @stackprogramer said:

        addresssimage:ParamGenSpiral.flagStart;

        you can't bind property to pure js value. It doesn't emit signal when it's updated - so your addresssimage can't know what it must be updated. It doesn't load new value of ParamGenSpiral.flagStart
        instead of this you can define your own property to bind or use this aproach http://stackoverflow.com/questions/27102055/bind-to-imported-javascript-property

        1 Reply Last reply
        0
        • stackprogramerS Offline
          stackprogramerS Offline
          stackprogramer
          wrote on last edited by stackprogramer
          #9

          i need some time ,i soon examine it and i reply the result thanks

          1 Reply Last reply
          0
          • stackprogramerS Offline
            stackprogramerS Offline
            stackprogramer
            wrote on last edited by
            #10

            i use Qt.binding that set property.

            Image {id:imagequestion
                            
                             property string addressImage:Qt.binding(function () { return ParamGenSpiral.addressImageOne })
            
            
                            source:imagequestion.addressImage;
                            width:100
                            height:100
            
                            anchors.centerIn: parent
            
                            RotationAnimation on rotation {
                                duration:600
                                loops: Animation.Infinite
                                from: 0
                                to:360
                              }
            
                        }
            

            when i use the source console print the value addressImageOne:

             console.debug(ParamGenSpiral.addressImageOne);
            

            but it return** undefined.**

            but my problem solve didn't solve.

            i too use method below but it result errors same before;

                            PropertyChanges { target: imagequestion; addressImage:Window.addressImageOne}
            
            
            C 1 Reply Last reply
            0
            • stackprogramerS stackprogramer

              i use Qt.binding that set property.

              Image {id:imagequestion
                              
                               property string addressImage:Qt.binding(function () { return ParamGenSpiral.addressImageOne })
              
              
                              source:imagequestion.addressImage;
                              width:100
                              height:100
              
                              anchors.centerIn: parent
              
                              RotationAnimation on rotation {
                                  duration:600
                                  loops: Animation.Infinite
                                  from: 0
                                  to:360
                                }
              
                          }
              

              when i use the source console print the value addressImageOne:

               console.debug(ParamGenSpiral.addressImageOne);
              

              but it return** undefined.**

              but my problem solve didn't solve.

              i too use method below but it result errors same before;

                              PropertyChanges { target: imagequestion; addressImage:Window.addressImageOne}
              
              
              C Offline
              C Offline
              crazymax
              wrote on last edited by
              #11

              @stackprogramer
              What is ParamGenSpiral in your code?
              Is it JS file which contain global variable addressImageOne?

              stackprogramerS 1 Reply Last reply
              0
              • C crazymax

                @stackprogramer
                What is ParamGenSpiral in your code?
                Is it JS file which contain global variable addressImageOne?

                stackprogramerS Offline
                stackprogramerS Offline
                stackprogramer
                wrote on last edited by
                #12

                @crazymax said:

                What is ParamGenSpiral in your code?
                Is it JS file which contain global variable addressImageOne?

                ParamGenSpiral is

                import "file.js" as ParamGenSpiral
                

                in file js we defined below:

                .pragma library
                var score_user=15;
                var tit=88;
                //we define selected image in this property
                var selectedImage;
                var flagStart=0;
                //we define the four image path here
                var  addressImageOne;
                var addressImageTwo;
                var addressImageThree;
                var  addressImageFour;
                
                
                
                function score() {
                   score_user= score_user+10;
                
                    return score_user
                
                }
                
                
                1 Reply Last reply
                0
                • stackprogramerS Offline
                  stackprogramerS Offline
                  stackprogramer
                  wrote on last edited by stackprogramer
                  #13

                  for more info we want to when i use tag type image we can set a value in varrible js and i can read value varrible in js.

                  Image{id:imageNum1
                  property string addrresimage:"qrc:///image.png"
                  ParamGenSpiral.addressImageOne:addrresimage;
                  source:addrresimage;
                  
                  }
                  
                  

                  my image show in my app,but my js varrible return undefined!!!!when i test ```

                  console.debug(ParamGenSpiral.addressImageOne);
                  C 1 Reply Last reply
                  0
                  • stackprogramerS stackprogramer

                    for more info we want to when i use tag type image we can set a value in varrible js and i can read value varrible in js.

                    Image{id:imageNum1
                    property string addrresimage:"qrc:///image.png"
                    ParamGenSpiral.addressImageOne:addrresimage;
                    source:addrresimage;
                    
                    }
                    
                    

                    my image show in my app,but my js varrible return undefined!!!!when i test ```

                    console.debug(ParamGenSpiral.addressImageOne);
                    C Offline
                    C Offline
                    crazymax
                    wrote on last edited by crazymax
                    #14

                    @stackprogramer
                    ParamGenSpiral is not a property of Image, so you can't
                    ParamGenSpiral.addressImageOne: addrresimage;

                    With this line ^^ I get qml error because it's not valid.

                    QQmlApplicationEngine failed to load component
                    qrc:/main.qml:21 Expected type name
                    

                    if you want to store some property of Image to js variable you can use smth like that

                    Image {
                            id:imagequestion
                            property string addrresimage: "qrc:///image.png"
                            // on property changed func updates ParamGenSpiral.addressImageOne variable to new value
                            onAddrresimageChanged: {
                                ParamGenSpiral.addressImageOne = addrresimage;
                                console.log("changed")
                            }
                            source:addrresimage;
                    
                            width:100
                            height:100
                    
                            anchors.centerIn: parent
                    
                            RotationAnimation on rotation {
                                duration:600
                                loops: Animation.Infinite
                                from: 0
                                to:360
                              }
                            Component.onCompleted:  {
                                ParamGenSpiral.addressImageOne = addrresimage;
                                console.log(addrresimage)
                            }
                        }
                    
                        Timer{
                            //prints out ParamGenSpiral.addressImageOne every 2 seconds
                            interval: 2000
                            repeat: true
                            running: true
                            onTriggered: {
                                console.log(ParamGenSpiral.addressImageOne)
                            }
                        }
                    
                    1 Reply Last reply
                    1
                    • stackprogramerS Offline
                      stackprogramerS Offline
                      stackprogramer
                      wrote on last edited by stackprogramer
                      #15

                      My problem is solved,this method solves my problem ,thanks very much for reply.

                       Component.onCompleted:  {
                                 
                              }
                      
                      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