The issue: QtScript is not bringing in all of the Qt classes into the global namespace. I'm developing a plugin as a proof of concept and I've been using Qt's networking facilities within QCAD, notably: QNetworkAccessManager, QNetworkReply, and QNetworkResponse. None of these types are available on Windows, but work fine on Linux and Mac. The only difference that might be possible is that I do not have Qt installed on Windows, only QCad.
The Javascript code:
include("./underscore.js"); // see underscorejs.org/, it works without modification and makes code so much nicer to work with /** * @class NetworkFactory allows sending and receiving of network responses on the REST API of the server. * @param config Configuration data for the server, this can also be a direct url no port may be specified. * @param {QUrl} config.url Server QUrl * @constructor */ function NetworkFactory(config) { if (_.isUndefined(config)) { throw new Error("Config must be specified"); } if (_.isString(config)) { // we assume it's a url then this.url = config; } this.mgr = new QNetworkAccessManager(); this.url = config.url; } NetworkFactory.prototype = Object.create(Object.prototype); NetworkFactory.prototype.constructor = NetworkFactory; /** * Creates a new QNetworkRequest with information stored in the factory. * @param path * @param queryParams * @returns {QNetworkRequest} */ NetworkFactory.prototype.request = function (path, queryParams) { this.request.name = "request"; if (_.isUndefined(path) || !_.isString(path)) { throw new Error("Path is not String: path = " + path); } var url = new QUrl(this.url); url.setPath(path); if (!_.isUndefined(queryParams)) { if (_.isString(queryParams)) { url.setEncodedQuery(Qurl.toPercentEncoding(queryParams)); } else if (_.isObject(queryParams)) { // convert to key-val string _.pairs(queryParams).forEach(function (v) { url.addQueryItem(v[0], v[1]); }); } else { // invalid debugger; throw new Error("Invalid postParams instance = " + queryParams); } } var out = new QNetworkRequest(url); out.setRawHeader("content-type", "application/json"); return out; }; /** * * @param {QNetworkReply} reply Created QNetworkReply * @param {Object} params Same parameter object used in NetworkFactory.get, etc. * @param {Function} [params.onError] Function called on error (`function (QNetworkReply.NetworkError)`) * @param {Function} [params.onSuccess] function called on successful completion (`function (QNetworkReply)`) */ NetworkFactory.connectReplySignals = function(reply, params) { if (!_.isUndefined(params.onError)) { if (_.isFunction(params.onError)) { reply['error(QNetworkReply::NetworkError)'].connect(params.onError); } } if (!_.isUndefined(params.onSuccess)) { if (_.isFunction(params.onSuccess)) { reply.finished.connect(function() { params.onSuccess(reply); }); } } }; /** * * @brief Creates a Network Query * @param {Object} params Parameters for a single network query. * @param {String} params.path Path on the server e.g. `/echo` * @param {Object} [params.body] Content placed into the body of the network request * @param {Function} [params.onSuccess] function called on successful completion (`function (QNetworkReply)`) * @param {Function} [params.onError] Function called on error (`function (QNetworkReply.NetworkError)`) * @param {Object} [params.queryParams] Post-style parameters, if a String will be put directly, if object will be converted * using key value map: i.e. `key1=val1&key2=val2` * * @returns {QNetworkReply} Response from QNetworkManager.get() */ NetworkFactory.prototype.get = function (params) { var req = this.request(params.path, params.queryParams); var reply = this.mgr.get(req); NetworkFactory.connectReplySignals(reply, params); return reply; }; // code continues with http callsThis code works without issue on Linux and Mac, but windows fails with:
Warning: RScriptHandlerEcma::RScriptHandlerEcma: script debugger enabled! Not recommended. Warning: RScriptHandlerEcma::eval: script engine exception: "ReferenceError: Can't find variable: QNetworkAccessManager" Warning: "<anonymous>()@C:/Program Files (x86)/QCAD/scripts/NetworkFactory.js:20" Warning: At least one uncaught exception: Warning: "<anonymous>()@C:/Program Files (x86)/QCAD/scripts/NetworkFactory.js:20" Debug: "<global>() at -1"Has anyone encountered this issue or had similar problems?