V4: Allow conversion from string to QByteArray when setting bindables

We allow it everywhere else, too.

Pick-to: 6.2 6.3 6.4
Fixes: QTBUG-105044
Change-Id: I714e5d501a780310791523c5f35a87681c69b1fb
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Ulf Hermann 2022-07-25 16:14:49 +02:00
parent 268d40b9f7
commit 00e95e3506
3 changed files with 13 additions and 1 deletions

View File

@ -2294,6 +2294,8 @@ bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, voi
case QMetaType::QByteArray:
if (const ArrayBuffer *ab = value.as<ArrayBuffer>())
*reinterpret_cast<QByteArray*>(data) = ab->asByteArray();
else if (const String *string = value.as<String>())
*reinterpret_cast<QByteArray*>(data) = string->toQString().toUtf8();
else
*reinterpret_cast<QByteArray*>(data) = QByteArray();
return true;

View File

@ -2214,6 +2214,7 @@ class BindableOnly : public QObject
{
Q_OBJECT
Q_PROPERTY(int score BINDABLE scoreBindable READ default WRITE default FINAL)
Q_PROPERTY(QByteArray data READ default WRITE default BINDABLE dataBindable FINAL)
QML_ELEMENT
public:
BindableOnly(QObject *parent = nullptr)
@ -2221,8 +2222,11 @@ public:
, m_score(4)
{}
QBindable<int> scoreBindable() { return QBindable<int>(&m_score); }
QBindable<QByteArray> dataBindable() { return QBindable<QByteArray>(&m_data); }
private:
QProperty<int> m_score;
QProperty<QByteArray> m_data;
};
void registerTypes();

View File

@ -7404,7 +7404,11 @@ void tst_qqmllanguage::bindableOnly()
QQmlEngine engine;
QQmlComponent c(&engine);
c.setData("import ABC\nBindableOnly {\nproperty int a: score\n}", QUrl(u"bindableOnly.qml"_s));
c.setData("import ABC\nBindableOnly {\n"
" property int a: score\n"
" data: \"sc\" + \"ore\"\n"
" objectName: data\n"
"}", QUrl(u"bindableOnly.qml"_s));
QScopedPointer<QObject> o(c.create());
QVERIFY(!o.isNull());
BindableOnly *bindableOnly = qobject_cast<BindableOnly *>(o.data());
@ -7413,6 +7417,8 @@ void tst_qqmllanguage::bindableOnly()
bindableOnly->scoreBindable().setValue(5);
QCOMPARE(bindableOnly->scoreBindable().value(), 5);
QCOMPARE(o->property("a").toInt(), 5);
QCOMPARE(o->property("data").value<QByteArray>(), QByteArray("score"));
QCOMPARE(o->objectName(), QStringLiteral("score"));
}
static void listsEqual(QObject *object, const char *method)