Qt-interest Archive, June 2007
Loading a js file as a QtScript
Message 1 in thread
Hi there,
I'm trying out this fancy new QtScript stuff and am trying to work out if I
have successfully loaded a js file for use as a QtScript.
I am using the following code
QFile scriptFile(":/imagelibrary.js");
scriptFile.open(QIODevice::ReadOnly);
m_engine.evaluate(scriptFile.readAll());
scriptFile.close();
QScriptValue ctor = m_engine.evaluate("ImageLibrary");
QScriptValue scriptUi =
m_engine.newQObject(this->m_ui_diveDashboard->comingSoonPicsLabel);
QScriptValue calc = ctor.construct(QScriptValueList() << scriptUi);
My question is, how do I know if the imagelibrary.js has been succesfully
loaded into the programs memory? It doesn't appear to throw an expection if I
try to load a non existant file. Also do I need to do something to
create :/imagelibrary.js as a "resource" or can I just leave it in my /src
directory?
Kind regards,
Declan
--
[ signature omitted ]
Message 2 in thread
On 23.06.07 20:36:14, Declan McGrath wrote:
> Hi there,
>
> I'm trying out this fancy new QtScript stuff and am trying to work out if I
> have successfully loaded a js file for use as a QtScript.
>
> I am using the following code
>
> QFile scriptFile(":/imagelibrary.js");
> scriptFile.open(QIODevice::ReadOnly);
> m_engine.evaluate(scriptFile.readAll());
> scriptFile.close();
>
> QScriptValue ctor = m_engine.evaluate("ImageLibrary");
> QScriptValue scriptUi =
> m_engine.newQObject(this->m_ui_diveDashboard->comingSoonPicsLabel);
> QScriptValue calc = ctor.construct(QScriptValueList() << scriptUi);
>
> My question is, how do I know if the imagelibrary.js has been succesfully
> loaded into the programs memory? It doesn't appear to throw an expection if I
> try to load a non existant file.
But open() returns a false value when it can't open the file. Qt doesn't
use exceptions for error reporting (partly due to historical reasons)
> Also do I need to do something to create :/imagelibrary.js as a
> "resource" or can I just leave it in my /src directory?
See the documentation about resources in Qt. You need to setup a .qrc
file
Andreas
--
[ signature omitted ]
Message 3 in thread
Cheers Andreas,
Thanks very much for your response though. Once I looked up that QT Resource
page like you said, everything made a lot more sense. I didn't realise it was
so simple.
What I'm really stuck on at the moment is I'd like to use qtscript just like
I'd just Javascript on a web page. So I have a QTextBrowser on my page which
I have filled with html which I would I would like manipulate via javascript.
Can I do this using qtscript? Something like the following qtscript
snippet...
function ImageLibrary(textBrowser)
{
this.textBrowser = textBrowser;
// This works fine
this.textBrowser.setHtml("<div id='myDiv' style='color:red;'>Some red
text</div>");
// But could I get something like this to work???
this.textBrowser.document().getElementById('myDiv').style="color:blue;";
}
Obviously this doesn't work, but is there way to do something like this?
P.S. I realise this is a bit crazy but I have my reasons :)
--
[ signature omitted ]
Message 4 in thread
Hmm... Just checked some recent posts to this list and found the "binding DOM
to javascript" post from a day or two ago. That might work for me.
Dec
On Saturday 23 June 2007 23:06, Declan McGrath wrote:
> Cheers Andreas,
>
> Thanks very much for your response though. Once I looked up that QT
> Resource page like you said, everything made a lot more sense. I didn't
> realise it was so simple.
>
> What I'm really stuck on at the moment is I'd like to use qtscript just
> like I'd just Javascript on a web page. So I have a QTextBrowser on my page
> which I have filled with html which I would I would like manipulate via
> javascript. Can I do this using qtscript? Something like the following
> qtscript snippet...
>
> function ImageLibrary(textBrowser)
> {
> this.textBrowser = textBrowser;
>
> // This works fine
> this.textBrowser.setHtml("<div id='myDiv' style='color:red;'>Some red
> text</div>");
>
> // But could I get something like this to work???
>
> this.textBrowser.document().getElementById('myDiv').style="color:blue;"; }
>
> Obviously this doesn't work, but is there way to do something like this?
>
> P.S. I realise this is a bit crazy but I have my reasons :)
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/
--
[ signature omitted ]