[SOLVED] import .js file in Qml without Resources
-
wrote on 14 Jan 2015, 09:56 last edited by
Hello guys,
I have this problem. I have a .js file in my filesystem (for example I have downloaded a new js in the folder where is the executable of my application) and I want execute it in my qml file without import it in Resources file.
How I can do it? It's possible?
main.qml
@
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2Window {
title: qsTr("Hello World")
width: 640
height: 480
visible: trueLoader { id: loadItem source: "test.qml" } Item { Component.onCompleted: loadItem.item }
}
@
test.qml
@import QtQuick 2.0
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.2import "test.js" as Test
Rectangle {
width: 100
height: 100
color: "red"Component.onCompleted: console.log(Test.testFunc())
}
@error: "qrc:/test.qml:5:1: Script qrc:/test.js unavailable"
-
wrote on 14 Jan 2015, 14:09 last edited by
I suppose that because the qml file is into a Resource file, you have to specify the complete path of the test.js.
In fact, from the error message, you can note that test.qml try to load test.js resolving the relative path to "qrc:/test.js"Try with full path.
import "fullPath/test.js" as Test -
wrote on 15 Jan 2015, 08:30 last edited by
My dir structure is:
workspace_QT - root dir
- build-test-Desktop_Qt_5_4_0_GCC_64bit-Debug - build dir (where is 'test.js')
- test - project dirI have modified it:
@import "../build-test-Desktop_Qt_5_4_0_GCC_64bit-Debug/test.js" as Test@
or
@import "qrc:/test.js" as Test@
but in both case it doesn't work :((( (file not found)
-
wrote on 15 Jan 2015, 08:44 last edited by
No, you did wrong.
the import is resolved at runtime, so you should put the path relative to the current working directory of the running app, not the relative path to the Qt project.
So, just for test, first put a full complete path and then use a runtime variable based on the current working directory.@
import "/full/path/from/root/test.js"
@In my project, I have a variable based on the current working directory of the app to resolve the paths into QML:
@
import backend.commonPath()+"/test.js"
@where backend is a C++ QObject with some utility methods exposed to QML side.
-
wrote on 15 Jan 2015, 08:57 last edited by
the result is the same; the complete path is:
"/home/tycos/workspace_QT/build-test-Desktop_Qt_5_4_0_GCC_64bit-Debug/test.js"
I've modified it in this way:
@import "/home/tycos/workspace_QT/build-test-Desktop_Qt_5_4_0_GCC_64bit-Debug/test.js" as Test@
and the error in "application output" is:
"qrc:/test.qml:5:1: Script qrc:/home/tycos/workspace_QT/build-test-Desktop_Qt_5_4_0_GCC_64bit-Debug/test.js unavailable
qrc:/home/tycos/workspace_QT/build-test-Desktop_Qt_5_4_0_GCC_64bit-Debug/test.js: File not found" -
You can use this trick to temporarily disable the QRC context: "link":https://github.com/sierdzio/closecombatfree/blob/master/src/ccfmain.cpp#L171.
-
wrote on 15 Jan 2015, 11:05 last edited by
[quote author="sierdzio" date="1421317439"]You can use this trick to temporarily disable the QRC context: "link":https://github.com/sierdzio/closecombatfree/blob/master/src/ccfmain.cpp#L171.[/quote]
It works!
main.qml
@
int main(int argc, char *argv[])
{
QApplication app(argc, argv);QQmlApplicationEngine engine; QQmlContext *context = engine.rootContext(); context->setBaseUrl(QUrl::fromLocalFile("")); engine.load(QUrl(QStringLiteral("main.qml"))); return app.exec();
}
@ -
Glad to hear it :-)
-
wrote on 21 Mar 2015, 12:27 last edited by
I have similar problem.
In my project, I cannot expose my main.qml file (must be binary & a part of exe). But the javascript file is user editable. Is this combination possible?