PDA

View Full Version : Why isn't it working? About external JS



Yonetici
23rd July 2012, 00:49
Hi,

I have 3 file, 2 QML and 1 JS.

JS:


.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?

wysota
23rd July 2012, 01:15
New value of what? Can you provide a minimal example reproducing the problem?

Yonetici
23rd July 2012, 01:19
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.

wysota
23rd July 2012, 08:10
No, I don't think it works that way.

Yonetici
23rd July 2012, 12:48
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

wysota
23rd July 2012, 14:05
I don't know what you are trying to do so it is hard to suggest a solution. How does your animation look like?

Yonetici
23rd July 2012, 14:17
on my Animation1.qml: (import "total.js" as Logic)


...
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)


...
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 :



.pragma library

for(var i = 0; i<1; i++){

var totaltxt = 0;
var mV;

if(mV == 5){
totaltxt++;
}
}

wysota
23rd July 2012, 14:38
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:

Text {
property int value: 0
id: total_txt
text: value
// ...
Behavior on value {
NumberAnimation {}
}
}

And then elsewhere in your app:

total_txt.value = 5

Yonetici
23rd July 2012, 14:44
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 :)

wysota
23rd July 2012, 15:16
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?

Yonetici
23rd July 2012, 15:37
because if it works, it is very easy to applicate anywhere

wysota
23rd July 2012, 15:41
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.

Yonetici
23rd July 2012, 16:56
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.

wysota
23rd July 2012, 17:15
That's quite easy. You don't need any extra javascript files for this...

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
}
}
}

}

Yonetici
23rd July 2012, 19:42
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

wysota
24th July 2012, 16:20
however your text and onExited in same file. My text and onExited at different qml files

It completely doesn't matter. See attachment.

Yonetici
24th July 2012, 16:24
It completely doesn't matter. See attachment.

thank you but attachment fails and I couldn't unzip tar file?

wysota
24th July 2012, 16:34
Here is a zip version.

Yonetici
24th July 2012, 16:40
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?

wysota
25th July 2012, 08:05
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
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
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
import QtQuick 1.1

Text {
text: root.score
}

Yonetici
26th July 2012, 00:07
thank you, I opened second zip file :)