How to extend the JavaScript environment in QML, reading a text file from js?
-
I need to read a text file from QML or js in QML. Can I use QScriptExtensionPlugin to write a plug-in extending QML JavaScript environment?
Thanks
-
No. You have to use the "QDeclarativeExtensionPlugin":http://doc.qt.nokia.com/latest/qdeclarativeengine.html. There is a "tutorial":http://doc.qt.nokia.com/latest/declarative-tutorials-extending-chapter6-plugins.html for doing so.
-
QDeclarativeExtensionPlugin extends QML environment only. I want to extend js in QML as well, something like "QML Global Object" - XMLHttpRequest. I found implementation of XMLHttpRequest in the QtDeclarative library source. I could go finish my own object allows to read a text file. However this would not be the right solution. We need to do it by utilizing plugins mechanism.
Can I somehow extend js environment with QScriptExtensionPlugin or in some other way without changing QtDeclarative library?I have the following QML:
import QtQuick 1.0
import "TextFile.js" as TextFileItem {
width: 300
height: 200Text {
id: idText1
x: 28
y: 48
color: "red"
text: ""
font.pixelSize: 50
}Timer {
interval: 3000; running: true; repeat: true;
onTriggered: {idText1.text = TextFile.readLine()}
}Component.onCompleted: Test.createFile()
}//TextFile.js
var file = null;function createFile()
{
file = new StreamReader();
file.open("c:\text.txt");
}function readLine()
{
var str = file.readLine();
return str;
} -
komar, can u pls enclose the code blocks within @ tags ? easier to read for all of us
-
QDeclarativeExtensionPlugin extends QML environment only. I want to extend js in QML as well, something like “QML Global Object” – XMLHttpRequest. I found implementation of XMLHttpRequest in the QtDeclarative library source. I could go finish my own object allows to read a text file. However this would not be the right solution. We need to do it by utilizing plugins mechanism.
Can I somehow extend js environment with QScriptExtensionPlugin or in some other way without changing QtDeclarative library?I have the following QML:
@
import QtQuick 1.0
import “TextFile.js” as TextFileItem {
width: 300
height: 200Text { id: idText1 x: 28 y: 48 color: “red” text: “” font.pixelSize: 50 } Timer { interval: 3000 running: true repeat: true onTriggered: {idText1.text = TextFile.readLine()} } Component.onCompleted: TextFile.createFile()
}
@@
var file = null;
function createFile()
{
file = new StreamReader();
file.open(“c:\text.txt”);
}function readLine()
{
var str = file.readLine();
return str;
}
@ -
komar thanks for the edited post ...
IMO, QML is best used for the UI of an application. Now using the view to do file reads might not be the best approach. Using javascript also might not be good for performance.
Why don't you read the file from Qt/C++, and let your QML pull this info ? -
Adding a "namespace" (with functions, etc) in a plugin isn't currently supported, but its something we'd like to support in a future version (I didn't have any luck finding the task in the tracker, but from memory there is one). The same is true for QScriptEngine access in general (see http://bugreports.qt.nokia.com/browse/QTBUG-11942).
You might still be able to use QDeclarativeExtensionPlugin for what you are after, by adding an object to the root context in "initializeEngine":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeextensionplugin.html#initializeEngine, though this isn't recommended for library-style plugins.
Regards,
Michael -
Thanks for all your answers. I will try to go with QDeclarativeExtensionPlugin and initializeEngine, and I am looking forward for new version of QT, guys.