Why isn't it working? About external JS
Hi,
I have 3 file, 2 QML and 1 JS.
JS:
Code:
.pragma library
var totaltxt = 0;
function totFunc(){
totaltxt = totaltxt + 1;
}
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?
Re: Why isn't it working? About external JS
New value of what? Can you provide a minimal example reproducing the problem?
Re: Why isn't it working? About external JS
Quote:
Originally Posted by
wysota
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.
Re: Why isn't it working? About external JS
No, I don't think it works that way.
Re: Why isn't it working? About external JS
Quote:
Originally Posted by
wysota
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
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?
Re: Why isn't it working? About external JS
on my Animation1.qml: (import "total.js" as Logic)
Code:
...
onExited: {
if(sRect.visible == true){
jet1.opacity = 0;
rectJet.opacity = 0;
jet1_1.visible = true;
jet1_2.visible = true;
jet1_sound.play();
if(jet1.opacity == 0){
Logic.mV = 5; // here is working but it can't change text of main.qml
}
}
}
...
main.qml: (import "total.js" as Logic)
Code:
...
Text {
id: total_txt
text: Logic.totaltxt // here is working but it can't understand if or not Animation.qml changes the value
anchors.topMargin: 2
anchors.leftMargin: 15
font.pixelSize: 50
color: "white"
}
Animation1 {
id: animation1
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.bottomMargin: 10
anchors.leftMargin: Math.floor(Math.random()*600+20)
}
...
total.js :
Code:
.pragma library
for(var i = 0; i<1; i++){
var totaltxt = 0;
var mV;
if(mV == 5){
totaltxt++;
}
}
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:
Code:
Text {
property int value: 0
id: total_txt
text: value
// ...
Behavior on value {
NumberAnimation {}
}
}
And then elsewhere in your app:
Code:
total_txt.value = 5
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 :)
Re: Why isn't it working? About external JS
Quote:
Originally Posted by
Yonetici
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?
Re: Why isn't it working? About external JS
because if it works, it is very easy to applicate anywhere
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.
Re: Why isn't it working? About external JS
Quote:
Originally Posted by
wysota
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.
Re: Why isn't it working? About external JS
That's quite easy. You don't need any extra javascript files for this...
Code:
import QtQuick 1.1
Rectangle {
id: root
width: 600
height: 400
property int score: 0
Text {
id: txt
anchors.top: parent.top
anchors.left: parent.left
anchors.margins: 16
text: score
}
Rectangle {
anchors.left: parent.left
anchors.right: parent.right
anchors.top: txt.bottom
anchors.bottom: parent.bottom
color: "lightyellow"
Rectangle {
width: 100
height: 100
radius: width/2
anchors.centerIn: parent
color: "blue"
MouseArea {
anchors.fill: parent
hoverEnabled: true
onExited: root.score = root.score+1
}
}
}
}
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
1 Attachment(s)
Re: Why isn't it working? About external JS
Quote:
Originally Posted by
Yonetici
however your text and onExited in same file. My text and onExited at different qml files
It completely doesn't matter. See attachment.
Re: Why isn't it working? About external JS
Quote:
Originally Posted by
wysota
It completely doesn't matter. See attachment.
thank you but attachment fails and I couldn't unzip tar file?
1 Attachment(s)
Re: Why isn't it working? About external JS
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?
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
Code:
import QtQuick 1.1
Rectangle {
id: root
width: 600
height: 400
property int score: 0
Score {
id: txt
anchors.top: parent.top
anchors.left: parent.left
anchors.margins: 16
}
Area {
anchors.left: parent.left
anchors.right: parent.right
anchors.top: txt.bottom
anchors.bottom: parent.bottom
}
}
Area.qml
Code:
import QtQuick 1.1
Rectangle {
color: "lightyellow"
Rectangle {
width: 100
height: 100
radius: width/2
anchors.centerIn: parent
color: "blue"
MouseArea {
anchors.fill: parent
hoverEnabled: true
onExited: root.score = root.score+1
}
}
}
Score.qml
Code:
import QtQuick 1.1
Text {
text: root.score
}