Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Why isn't it working? About external JS

  1. #1
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    3
    Platforms
    Windows

    Default Why isn't it working? About external JS

    Hi,

    I have 3 file, 2 QML and 1 JS.

    JS:

    Qt Code:
    1. .pragma library
    2.  
    3. var totaltxt = 0;
    4. function totFunc(){
    5. totaltxt = totaltxt + 1;
    6. }
    To copy to clipboard, switch view to plain text mode 

    and first QML file includes text: Logic.totaltxt and so, it shows "0" as what we expect. However, although other QML file includes Logic.totFunc(); , it doesn't work. I mean, when second qml works, I expect new value must be 1 instead of zero.

    I did all imports to do files.

    Any idea?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why isn't it working? About external JS

    New value of what? Can you provide a minimal example reproducing the problem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    3
    Platforms
    Windows

    Default Re: Why isn't it working? About external JS

    Quote Originally Posted by wysota View Post
    New value of what? Can you provide a minimal example reproducing the problem?
    I mean new text of text: Logic.totaltxt,I think when Logic.totFunc() works, totaltxt should be totaltxt = 1, then text = 1, and so we will see 1 instead of 0 on the screen.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why isn't it working? About external JS

    No, I don't think it works that way.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    3
    Platforms
    Windows

    Default Re: Why isn't it working? About external JS

    Quote Originally Posted by wysota View Post
    No, I don't think it works that way.
    what can you suggest me? I found out that because same .js is used for 2 different qml files, it doesn't work properly. Because in QT, it protect to same variables.

    But, how can I do that? Because, one of qml files plays animation to make score, and second one shows the score. Can you imagine something?

    ----------

    edit: I also found another same question on Internet, you may check it:

    http://goo.gl/7dm7d
    Last edited by Yonetici; 23rd July 2012 at 14:19.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why isn't it working? About external JS

    I don't know what you are trying to do so it is hard to suggest a solution. How does your animation look like?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    3
    Platforms
    Windows

    Default Re: Why isn't it working? About external JS

    on my Animation1.qml: (import "total.js" as Logic)

    Qt Code:
    1. ...
    2. onExited: {
    3. if(sRect.visible == true){
    4. jet1.opacity = 0;
    5. rectJet.opacity = 0;
    6. jet1_1.visible = true;
    7. jet1_2.visible = true;
    8. jet1_sound.play();
    9. if(jet1.opacity == 0){
    10. Logic.mV = 5; // here is working but it can't change text of main.qml
    11. }
    12. }
    13. }
    14. ...
    To copy to clipboard, switch view to plain text mode 

    main.qml: (import "total.js" as Logic)

    Qt Code:
    1. ...
    2. Text {
    3. id: total_txt
    4. text: Logic.totaltxt // here is working but it can't understand if or not Animation.qml changes the value
    5. anchors.topMargin: 2
    6. anchors.leftMargin: 15
    7. font.pixelSize: 50
    8. color: "white"
    9. }
    10.  
    11. Animation1 {
    12. id: animation1
    13. anchors.bottom: parent.bottom
    14. anchors.left: parent.left
    15. anchors.bottomMargin: 10
    16. anchors.leftMargin: Math.floor(Math.random()*600+20)
    17. }
    18. ...
    To copy to clipboard, switch view to plain text mode 

    total.js :

    Qt Code:
    1. .pragma library
    2.  
    3. for(var i = 0; i<1; i++){
    4.  
    5. var totaltxt = 0;
    6. var mV;
    7.  
    8. if(mV == 5){
    9. totaltxt++;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why isn't it working? About external JS

    I still don't understand what you are trying to do but I belive you approach the problem incorrectly. You're thinking in an imperative way rather than using the declarative engine at hand. If you want to animate the value of some label then you can do it this way:

    javascript Code:
    1. Text {
    2. property int value: 0
    3. id: total_txt
    4. text: value
    5. // ...
    6. Behavior on value {
    7. NumberAnimation {}
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    And then elsewhere in your app:

    javascript Code:
    1. total_txt.value = 5
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    3
    Platforms
    Windows

    Default Re: Why isn't it working? About external JS

    Thank you for feedback but it isn't actually what I need. Because I want to do that: when my animation plays, when onExit works, a value (mV = 5) will turn what I added and javascript code will give this value to mV in total.js ; then because javascript code works, (text: Logic.totaltxt) totaltxt will take 1 instead of 0 and we will see 1 on screen instead of 0.

    I think that I can't tell what I mean because I'm new on qml

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why isn't it working? About external JS

    Quote Originally Posted by Yonetici View Post
    Thank you for feedback but it isn't actually what I need. Because I want to do that: when my animation plays, when onExit works, a value (mV = 5) will turn what I added and javascript code will give this value to mV in total.js ; then because javascript code works, (text: Logic.totaltxt) totaltxt will take 1 instead of 0 and we will see 1 on screen instead of 0.
    What do you need it for? What is the ultimate goal you are trying to achieve? Why do you need the value to be a js variable and not a qml property?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    3
    Platforms
    Windows

    Default Re: Why isn't it working? About external JS

    because if it works, it is very easy to applicate anywhere

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why isn't it working? About external JS

    The thing is it doesn't work And you are really not helping me solve your problem. The code you posted last time makes completely no sense and your explanation doesn't really explain what you are trying to do. Your "because if it works, it is very easy to applicate anywhere" also doesn't add anything to the subject. Please state what you are trying to achieve instead of trying to fix this nonsense code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    3
    Platforms
    Windows

    Default Re: Why isn't it working? About external JS

    Quote Originally Posted by wysota View Post
    The thing is it doesn't work And you are really not helping me solve your problem. The code you posted last time makes completely no sense and your explanation doesn't really explain what you are trying to do. Your "because if it works, it is very easy to applicate anywhere" also doesn't add anything to the subject. Please state what you are trying to achieve instead of trying to fix this nonsense code.
    ok, I have a animation qml file, Anime.qml, and I added it to Main.qml as Anime { ...some code... }. In this Anime.qml, if mouse exit from mouse area (I did it with onExited{..some code...}), some opacity and visibilty change and they works well. However, I want to add a code for SCORE in onExited{...}, because I want to increase score +1, when onExited works.

    This SCORE is in Main.qml like Text{... text = bla bla ...} , and normally it shows 0. I want that, if onExited condition ok in Anime.qml, this SCORE text in Main.qml should increase +1, and we see 1 instead of 0 on the screen.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why isn't it working? About external JS

    That's quite easy. You don't need any extra javascript files for this...

    javascript Code:
    1. import QtQuick 1.1
    2.  
    3. Rectangle {
    4. id: root
    5. width: 600
    6. height: 400
    7.  
    8. property int score: 0
    9.  
    10. Text {
    11. id: txt
    12. anchors.top: parent.top
    13. anchors.left: parent.left
    14. anchors.margins: 16
    15. text: score
    16. }
    17.  
    18. Rectangle {
    19. anchors.left: parent.left
    20. anchors.right: parent.right
    21. anchors.top: txt.bottom
    22. anchors.bottom: parent.bottom
    23. color: "lightyellow"
    24.  
    25. Rectangle {
    26. width: 100
    27. height: 100
    28. radius: width/2
    29. anchors.centerIn: parent
    30. color: "blue"
    31.  
    32. MouseArea {
    33. anchors.fill: parent
    34. hoverEnabled: true
    35. onExited: root.score = root.score+1
    36. }
    37. }
    38. }
    39.  
    40. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #15
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    3
    Platforms
    Windows

    Default Re: Why isn't it working? About external JS

    ok I will try and give you information if it works for me, thank you

    ----------

    edit:

    however your text and onExited in same file. My text and onExited at different qml files


    .....

    yes, it isn't applicable for me
    Last edited by Yonetici; 23rd July 2012 at 20:42.

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why isn't it working? About external JS

    Quote Originally Posted by Yonetici View Post
    however your text and onExited in same file. My text and onExited at different qml files
    It completely doesn't matter. See attachment.
    Attached Files Attached Files
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #17
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    3
    Platforms
    Windows

    Default Re: Why isn't it working? About external JS

    Quote Originally Posted by wysota View Post
    It completely doesn't matter. See attachment.
    thank you but attachment fails and I couldn't unzip tar file?

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why isn't it working? About external JS

    Here is a zip version.
    Attached Files Attached Files
    Last edited by wysota; 24th July 2012 at 17:39.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. The following user says thank you to wysota for this useful post:

    Yonetici (24th July 2012)

  20. #19
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    3
    Platforms
    Windows

    Default Re: Why isn't it working? About external JS

    thank you again but the problem is that test.tar file can't be opened or unzipped. Is there something wrong with me? or tar file is wrong?

  21. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why isn't it working? About external JS

    I attached a zip file. It's not tarred. The archive is correct, I just opened it. I was able to open the previous one too however I know that this forum does strange things to my tar.gz attachments so I uploaded a zipped version.

    Here is the inlined code:

    main.qml
    javascript Code:
    1. import QtQuick 1.1
    2.  
    3. Rectangle {
    4. id: root
    5. width: 600
    6. height: 400
    7.  
    8. property int score: 0
    9.  
    10. Score {
    11. id: txt
    12. anchors.top: parent.top
    13. anchors.left: parent.left
    14. anchors.margins: 16
    15. }
    16.  
    17. Area {
    18. anchors.left: parent.left
    19. anchors.right: parent.right
    20. anchors.top: txt.bottom
    21. anchors.bottom: parent.bottom
    22. }
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

    Area.qml
    javascript Code:
    1. import QtQuick 1.1
    2.  
    3. Rectangle {
    4. color: "lightyellow"
    5.  
    6. Rectangle {
    7. width: 100
    8. height: 100
    9. radius: width/2
    10. anchors.centerIn: parent
    11. color: "blue"
    12.  
    13. MouseArea {
    14. anchors.fill: parent
    15. hoverEnabled: true
    16. onExited: root.score = root.score+1
    17. }
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    Score.qml
    javascript Code:
    1. import QtQuick 1.1
    2.  
    3. Text {
    4. text: root.score
    5. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to use an external variable ?
    By Computer Hater in forum General Programming
    Replies: 10
    Last Post: 25th June 2011, 05:45
  2. QProcess & Working with External Programs
    By jstippey in forum Qt Programming
    Replies: 2
    Last Post: 20th December 2010, 21:04
  3. Using external libraries
    By Handi in forum Newbie
    Replies: 5
    Last Post: 9th December 2010, 23:51
  4. Use external DLL ?
    By pl01 in forum Newbie
    Replies: 5
    Last Post: 10th November 2010, 15:48
  5. add external api
    By adamatic in forum Qt Programming
    Replies: 6
    Last Post: 16th April 2009, 11:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.