Globals
Strings and other QtScript objects that Qt Scripting can cast directly can be stored in objects.
All global objects do not automatically persist when the User closes Tw.
If you have difficulties, the safest bet are simple things like numerical values and strings - and don't forget JSON structures.
Once stored (with a name created by the script writer), the global can be tested for (by name - does it exist already? - hasGlobal) and be re-read back into QtScript.
Globals are stored for as long as the Tw editor is still running, and should be retrieved and formatted (possibly json) and saved to disk if required for later use.
There are two types of Globals.
hasGlobal(QString)
TW.app.hasGlobal("my_Globaal_01");
TW.script.hasGlobal("colour_Name");
Returns a boolean (true or false ) showing whether the global has already been created and set.
setGlobal(QString)
TW.app.setGlobal("font_name", yourObject);
TW.script.setGlobal("anyThing_you_like", anotherObject);
The object should be one that Qt can cast (convert) from QtScript see table here:
Default Conversion from Qt Script to C++
If you have difficulties, the safest bet are simple things like numerical values and strings.
getGlobal(QString)
var myGlobalVariable = TW.app.getGlobal("font_name");
var myScriptVariable = TW.script.getGlobal("anyThing_you_like");
Returns the global named, to script, and stores it in the variable name you supply.
Use hasGlobal first if unsure of its existence.
TW.app.unsetGlobal("foo")
and
TW.script.unsetGlobal("foo")
References:
> How do I get rid of a global variable I've set
http://tug.org/pipermail/texworks/2012q2/005453.html
Oops, I think that was overlooked when the "globals" framework was
implemented.
I added the two functions TW.app.unsetGlobal("foo") and
TW.script.unsetGlobal("foo") in r994. They should do just what you want.
Cheers,
Stefan
void TWApp::setGlobal(const QString& key, const QVariant& val)
Created with the Personal Edition of HelpNDoc: Full featured Documentation generator