-1

I am new to Qt/Qml and javascript. Let's say I have a javascript file which contains a table of parameters. The javascript file is used in Qml and it is located in the application directory. e.g. C:\Program Files\QtApp\the_table.js.

If end customer installed the Qt app, they want to manually edit the javascript file and use it with the installed application. ( No need to recompile the app source code). How can I make it work?

I have tried to update the javascript file manually, but when I try to open the app again, the "updated entry" in the javascript file is not displayed in the application.

-----------------8<-----------------------

Updates:

  • Javascript file included in the qml resource will not be editable by end user without recompiling the app.
  • Javascript in qml will hold mostly the logic, not the data.
  • It's better to use the JSON file to store the configuration data that can be modified by the end user.
  • Qt 5.0 and newer version has support for reading JSON file. QJsonDocument.
Heidi
  • 161
  • 5
  • 16
  • 1
    If your javascript file is accessed through `resources.qrc` you need to recompile. Otherwise, are you sure to modify the file which is in the build directory? – lolo Oct 17 '16 at 13:23
  • @user2436719 aha, you are right. the current javascript file is in resources.qrc. How to change to access the javascript file from the directory where the exetuable is located ? – Heidi Oct 17 '16 at 13:25
  • Maybe you can work on this post [qml-how-to-specify-image-file-path-relative-to-application-folder](http://stackoverflow.com/questions/27549708/qml-how-to-specify-image-file-path-relative-to-application-folder) – lolo Oct 17 '16 at 13:32

2 Answers2

1

It sounds like the flexibility you require should be implemented at application level rather than externally. If it is just a "table of parameters" there are plenty of ways to do it in JS.

Due to implementation details you most likely don't want to bother with, that would be much preferable to updating the actual JS file.

Consider the usage of import Qt.labs.settings 1.0 which will allow you to store and make changes to settings that persist across application runs. That would be marginally easier than parsing config files. That being said, doing so is very much a viable option, you just have to interface QDataStream to QML.

That's the best I can do without knowledge of specific requirements and usage scenario.

OK, after a quick test, it seems that it is possible to import a file from the file system, you just need to:

import "file:///pathToYourExecutable/YourScript.js" as YourScript

Then you can use YourScript in QML. However, I would not recommend using this, as it invites opportunities to mess up with the application. The user shouldn't really be editing any files to make changes, just seems like bad design. Whatever it is that you want to achieve, there is most likely a better way to do it.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • hmm, the javascript file contains an array actually, the array was accessed by for-loop in Qml files in the app. – Heidi Oct 17 '16 at 13:40
  • So this javascript file should be editable by end user. in Qml there is "import the_table.js as ParamTable" statement. – Heidi Oct 17 '16 at 13:41
  • currently it is inside the resources.qrc in the source code. how to change the Qml files that can find the correct javascript file( will be located in the same directory where the App.exe file is). – Heidi Oct 17 '16 at 13:42
  • You can't make changes to anything that's in a resource file after compilation, that much should be obvious. Sounds like you need to consider what I describe in the third sentence of the third paragraph of my answer. Expose a data stream and read in data from a config file each time the application starts. – dtech Oct 17 '16 at 13:43
  • :( cannot understand how to use Qt.labs.setting 1.0 – Heidi Oct 17 '16 at 13:45
  • @Heidi settings won't be a good candidate for arbitrary data, it would only work for a fixed data scheme. Refresh the page to see the edit of my previous comment. – dtech Oct 17 '16 at 13:46
  • It's the Qml that access the javascript file. any way in Qml that I can access a file and loop the table? like in Qt, there is QDir::currentPath(), but don't know how Qml works... – Heidi Oct 17 '16 at 13:46
  • Thanks, but: import "file:///pathToYourExecutable/YourScript.js" as YourScript, this seems doesn't allow to specify the installation path? the path may different on different user's PC. – Heidi Oct 17 '16 at 14:19
  • generally speaking, if a javascript file is user configurable file. how to import/use/access it in Qml ? any generally recommended ways and examples ? – Heidi Oct 17 '16 at 14:20
  • @Heidi As I repeated many times - you are doing it wrong. Although JS files can contain data, its intended purpose is to contain logic. If it is data, store it in a regular binary file and read in from it via QDataStream. You can only import JS files from a path string, be that from the QRC or from filesystem, but you cannot import from a variable that corresponds to the application path. Thus it won't work unless your import path is a hard coded string. – dtech Oct 17 '16 at 14:35
  • Thank you very much. solved by changing to JSON file. – Heidi Oct 21 '16 at 07:51
1

Solved by changing to use json file to store the data and then access the json file in QML.

The class to read the json file from the disk is JsonFile class in Qt, check this link: https://forum.qt.io/topic/39756/qml-and-js-reading-json-file-and-change-content-on-the-go

Heidi
  • 161
  • 5
  • 16