add convenience function 'qml:hex' for storing qint64 values in QML/JS

This commit is contained in:
pls.153 2023-09-19 12:45:22 +02:00
parent 3d23e6d2f4
commit eff04b90f2
5 changed files with 27 additions and 6 deletions

View file

@ -7,7 +7,7 @@
#include <QQuickView>
#include <QDebug>
const char LQML::version[] = "23.9.1"; // September 2023
const char LQML::version[] = "23.9.2"; // September 2023
extern "C" void ini_LQML(cl_object);

View file

@ -20,11 +20,16 @@ static QVariant toVariant(const QJSValue& value) {
#else
const int type = var.typeId();
#endif
if (type == QMetaType::Double) {
// workaround for uint32, see '>>>0' in QML
quint32 uint = var.toUInt();
if (uint == var.toDouble()) {
return QVariant(uint);
if (type == QMetaType::QString) {
QString s(var.toString());
// numbers passed with '(qml:hex number)' to QML (and stored there as
// strings) are automatically converted back to a number (qint64).
if (s.startsWith("#x")) {
bool ok;
qint64 num = s.mid(2).toLongLong(&ok, 16);
if (ok) {
return QVariant(num);
}
}
}
return var;