PSA: Underscore.js works well in QtScript

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
Nava2
Junior Member
Posts: 15
Joined: Sun Oct 26, 2014 6:01 pm

PSA: Underscore.js works well in QtScript

Post by Nava2 » Wed Dec 24, 2014 7:20 pm

Underscore.js works great in QtScript.

Underscore brings stream-based iterations with a simple map-reduce style framework.

For example, to get all of the texts in a QComboBox:
var texts = _.range(combo.count).map(function (i) { return combo.itemText(i); }); // texts is an array of Strings
Code to insert into a sorted combobox based on the texts (in non-QtScript, this is done via a QSortProxyModel which is unavailable in QtScript):
var insertCBItem = function (cb, text) {
    var texts = _.range(cb.count).map(function (i) { return cb.itemText(i); });
    var insertIdx = _.sortedIndex(texts, text, String.localeAwareCompare);

    cb.insertItem(insertIdx, text, data);
};
The only reservation is that the utility functions that use setTimeout() are unlikely to work due to QtScript not implementing them.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”