Globals

Parent Previous Next

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.


    1. Globals that are available from and to any script while the Tw editor is still open, see Application Wide Globals


    1. And globals that are assigned to a particular script, they are available again during the current Tw session, if the script that created them is used again. Other scripts do not have access to globals stored by another script,  see TW.script.hasGlobal(QString)  TW.script.setGlobal(QString,QVariant)  and TW.script.getGlobal(QString)  


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.


Unsetting Global variables


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

http://www.google.com/codesearch/p?hl=en#TCAOwTxChU8/trunk/src/TWApp.h&q=getGlobal%20package:http://texworks%5C.googlecode%5C.com&sa=N&cd=1&ct=rc&l=147


http://www.google.com/codesearch/p?hl=en#TCAOwTxChU8/trunk/src/TWApp.cpp&q=setGlobal%20package:http://texworks%5C.googlecode%5C.com&l=1177

void TWApp::setGlobal(const QString& key, const QVariant& val)

Created with the Personal Edition of HelpNDoc: Full featured Documentation generator