PDA

View Full Version : Pass data from QT to javascript function



thanhluanbk88
3rd March 2011, 04:09
I have a question that i really don't know.
Here is the my code.


QString from;
QFile file( ":/direction.js");
if ( !file.open(QIODevice::ReadOnly | QIODevice::Text) )
{
msg.setText("Fail");
msg.exec();
}
else
{
from = "6";
QString functions = file.readAll();
QWebFrame* frame = ui->webView->page()->mainFrame();
frame->evaluateJavaScript( functions );

QString script = QString("Direction(%1);").arg( from );
frame->evaluateJavaScript(script);
}

and the content of direction.js like :


function Direction (from)

{

alert("This is Direction");
alert(from);

}



But i am very confused with the value of QString from.
when i put it : from="8".It run OK
But when i put it : from="a".It can not run.
If you know this problem.Please explain it for me.
Thanks and best regards!

Lykurg
3rd March 2011, 05:28
I am not a javascript guy but isn't Direction(3); correct while Direction(a); needs quotation marks?

thanhluanbk88
3rd March 2011, 10:10
Thanks very much. I have to add in QT as follow :
from = "\""+myString+"\"";

Lykurg
3rd March 2011, 10:40
Also not sure again, but simple quotations marks should be allowed as well. Thus there is no need to escape the quotation marks which makes the code more readable (in my opinion):
from = QString("'%1'").arg(myString);