qml-commonjs
qml-commonjs copied to clipboard
Experiment in adding CommonJS support to QML.
DISCONTINUED
Notice: Unfortunately I can not commit to supporting and further development of this project. Please check Quickly which provides similar, or better functionality. If you are still interested in becoming an owner / maintainer please submit an issue and I will get back to you.
QML CommonJS
Experiment in adding CommonJS module support to QML.
Changelog
Version 0.2
- Fixed compatibility with Qt 5.4+ (see changes below for details).
- CHANGED paths for
CommonJS.requirecalls from QML are now resolved relative toQmlEngine::baseUrl()because evaluatingQt.resolvedUrl('.')no longer works from CPP code for some reason. - CHANGED evaluated module is now wrapped in an IIFE to avoid weird problem where function with a name same a property on a global object (e.g.
hasOwnProperty) would result in an error.
Version 0.1
- Full modules support
assertmodulesetTimeout/clearTimeout/setInterval/clearInterval- experimental support for
util,events,path,urlandquerystringmodules.
When accessing top-level functions (like setTimeout) from QML, you have to use CommonJS. prefix because Qt doesn't allow defining global functions.
Installation
- Make sure you have Qt 5.4+ installed.
- Clone this repository.
- Then run
qmake && make && make install
Usage
After that you should be able to just import CommonJS as regular QML library and use it in your code:
import CommonJS 0.2
import QtQuick 2.0
Rectangle {
Component.onCompleted: {
CommonJS.setTimeout(function(){
// built-in module
var example = CommonJS.require('path');
// relative to QML base url
var example = CommonJS.require('./example.js');
// relative to this file
var example = CommonJS.require(Qt.resolvedUrl('./example.js'));
});
}
}