QML Script Console: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
(Convert ExpressionEngine links)
Line 46: Line 46:
</code>
</code>


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":https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date:
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]:
<code>
<code>
var now = new Date(); parent.text = now.getHours().toPrecision(2) + ":" + now.getMinutes().toPrecision(2)
var now = new Date(); parent.text = now.getHours().toPrecision(2) + ":" + now.getMinutes().toPrecision(2)

Revision as of 15:14, 4 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

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

qmlviewer + qmlscriptconsole

The QML 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.

To get started, download and build the plugin:

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

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

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

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

parent.text = "Hey, JavaScript!"

The 'test.qml' contains the following:

import QtQuick 1.0
import com.nokia.ScriptConsole 1.0

Text {
 text: "Hello, ECMAScript!"
 ScriptConsole { title: "Inside the text component" }
}

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:

for (var n in parent) print(n, typeof(n))

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:

var now = new Date(); parent.text = now.getHours().toPrecision(2) + ":" + now.getMinutes().toPrecision(2)

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

title = "Cool terminal!"