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.
Experiment in adding CommonJS module support to QML.
- Fixed compatibility with Qt 5.4+ (see changes below for details).
- CHANGED paths for
CommonJS.require
calls 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.
- Full modules support
assert
modulesetTimeout
/clearTimeout
/setInterval
/clearInterval
- experimental support for
util
,events
,path
,url
andquerystring
modules.
When accessing top-level functions (like setTimeout
) from QML, you have to use CommonJS.
prefix because Qt doesn't allow defining global functions.
- Make sure you have Qt 5.4+ installed.
- Clone this repository.
- Then run
qmake && make && make install
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'));
});
}
}