QML Script Console: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Inspecting Qt Quick Applications using a custom made <span class="caps">QML</span> Script Console component.=
[[Category:Developing_with_Qt::Qt_Quick]]<br />[[Category:Developing_with_Qt::Qt_Quick::QML]]<br />[[Category:Developing_with_Qt::QtScript]]<br />[[Category:Developing_with_Qt::QtScriptConsole]]


[[Image:screenshot.png|qmlviewer + qmlscriptconsole]]
= Inspecting Qt Quick Applications using a custom made QML Script Console component. =


The <span class="caps">QML</span> Script Console provides a “ScriptConsole” component. For each instance of a ScriptConsole inside your Qt Quick application a script editor window will be opened which allows you to inject code into the running application. Thereby you can test expression in the running application context.
[[Image:https://raw.github.com/frankencode/qmlscriptconsole/master/screenshot.png|qmlviewer + qmlscriptconsole]]


To get started, download and build the plugin:<br />
The QML Script Console provides a &quot;ScriptConsole&amp;quot; component. For each instance of a ScriptConsole inside your Qt Quick application a script editor window will be opened which allows you to inject code into the running application. Thereby you can test expression in the running application context.


Once your build is complete, try to open the ‘test.qml’ shipped with the console.<br />
To get started, download and build the plugin:<br /><code><br />curl -L https://github.com/frankencode/qmlscriptconsole/archive/master.zip &gt; qmlscriptconsole.zip &amp;&amp; unzip qmlscriptconsole.zip<br />cd qmlscriptconsole-master<br />qmake-qt4 &amp;&amp; make -j2<br /></code>


Now try to modify the application state by entering something like:<br />
Once your build is complete, try to open the 'test.qml' shipped with the console.<br /><code><br />qmlviewer -I $PWD test.qml<br /># You need the Qt4 qmlviewer here!, e.g. try: dpkg -L qt4-qmlviewer|grep bin/qmlviewer<br /></code>


The ‘test.qml’ contains the following:<br />
Now try to modify the application state by entering something like:<br /><code><br />parent.text = &quot;Hey, JavaScript!&quot;<br /></code>


As you can see, the script console is just another component. You can use the standard ‘print()’ function to inspect the <span class="caps">QML</span> object tree. For instance the following prints all attributes and methods which are exposed to the script environment:<br />
The 'test.qml' contains the following:<br /><code><br />import QtQuick 1.0<br />import com.nokia.ScriptConsole 1.0


Especially when you are building more complex expressions you may want to try them out first. For instance, the following displays the current time using the [https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date builtin Date object] ''[developer.mozilla.org]'':<br />
Text {<br /> text: &quot;Hello, ECMAScript!&quot;<br /> ScriptConsole { title: &quot;Inside the text component&amp;quot; }<br />}<br /></code>


As the ‘ScriptConsole’ itself is also a <span class="caps">QML</span> component, it is not protected against dynamic modification:<br />
As you can see, the script console is just another component. You can use the standard 'print()' function to inspect the QML object tree. For instance the following prints all attributes and methods which are exposed to the script environment:<br /><code><br />for (var n in parent) print(n, typeof(n))<br /></code>


===Categories:===
Especially when you are building more complex expressions you may want to try them out first. For instance, the following displays the current time using the &quot;builtin Date object&amp;quot;:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date:<br /><code><br />var now = new Date(); parent.text = now.getHours().toPrecision(2) + &quot;:&quot; + now.getMinutes().toPrecision(2)<br /></code>


* [[:Category:Developing with Qt|Developing_with_Qt]]
As the 'ScriptConsole' itself is also a QML component, it is not protected against dynamic modification:<br /><code><br />title = &quot;Cool terminal!&quot;<br /></code>
** [[:Category:Developing with Qt::QtScript|QtScript]]
** [[:Category:Developing with Qt::QtScriptConsole|QtScriptConsole]]
* [[:Category:Developing with Qt::Qt Quick|Qt_Quick]]
 
* [[:Category:Developing with Qt::Qt Quick::QML|QML]]

Revision as of 09:56, 24 February 2015




Inspecting Qt Quick Applications using a custom made QML Script Console component.

qmlviewer + qmlscriptconsole

The QML Script Console provides a "ScriptConsole&quot; component. For each instance of a ScriptConsole inside your Qt Quick application a script editor window will be opened which allows you to inject code into the running application. Thereby you can test expression in the running application context.

To get started, download and build the plugin:

<br />curl -L https://github.com/frankencode/qmlscriptconsole/archive/master.zip &gt; qmlscriptconsole.zip &amp;&amp; unzip qmlscriptconsole.zip<br />cd qmlscriptconsole-master<br />qmake-qt4 &amp;&amp; make -j2<br />

Once your build is complete, try to open the 'test.qml' shipped with the console.

<br />qmlviewer -I $PWD test.qml<br /># You need the Qt4 qmlviewer here!, e.g. try: dpkg -L qt4-qmlviewer|grep bin/qmlviewer<br />

Now try to modify the application state by entering something like:

<br />parent.text = &quot;Hey, JavaScript!&quot;<br />

The 'test.qml' contains the following:

<br />import QtQuick 1.0<br />import com.nokia.ScriptConsole 1.0

Text {<br /> text: &quot;Hello, ECMAScript!&quot;<br /> ScriptConsole { title: &quot;Inside the text component&amp;quot; }<br />}<br />

As you can see, the script console is just another component. You can use the standard 'print()' function to inspect the QML object tree. For instance the following prints all attributes and methods which are exposed to the script environment:

<br />for (var n in parent) print(n, typeof(n))<br />

Especially when you are building more complex expressions you may want to try them out first. For instance, the following displays the current time using the "builtin Date object&quot;:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date:

<br />var now = new Date(); parent.text = now.getHours().toPrecision(2) + &quot;:&quot; + now.getMinutes().toPrecision(2)<br />

As the 'ScriptConsole' itself is also a QML component, it is not protected against dynamic modification:

<br />title = &quot;Cool terminal!&quot;<br />