diff options
author | Sami Shalayel <sami.shalayel@qt.io> | 2023-06-07 16:46:01 +0200 |
---|---|---|
committer | Sami Shalayel <sami.shalayel@qt.io> | 2023-06-09 15:46:31 +0200 |
commit | 0978bb32a94473d74fa62ef96781278bcad863e7 (patch) | |
tree | d4121d89d4cb68236e45066d0e7070a18dcb4c2e | |
parent | 87d6eaae22b3881ea6981ea697af722b2fc79db8 (diff) |
qmltc: do not use the implicit QUrl constructor in generated code
Instead, call the QUrl constructor explicitly when constructing url's
from stringliterals.
Add a test to show that binding string (not literal strings) to Url
already uses the explicit QUrl constructor.
Fixes: QTBUG-113875
Change-Id: I838437d9d45d57f416d78e71d718725975bee264
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
-rw-r--r-- | tests/auto/qml/qmltc/CMakeLists.txt | 2 | ||||
-rw-r--r-- | tests/auto/qml/qmltc/QmltcTests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/auto/qml/qmltc/QmltcTests/stringToUrl.qml | 12 | ||||
-rw-r--r-- | tests/auto/qml/qmltc/tst_qmltc.cpp | 14 | ||||
-rw-r--r-- | tests/auto/qml/qmltc/tst_qmltc.h | 1 | ||||
-rw-r--r-- | tools/qmltc/qmltccompiler.cpp | 8 |
6 files changed, 36 insertions, 2 deletions
diff --git a/tests/auto/qml/qmltc/CMakeLists.txt b/tests/auto/qml/qmltc/CMakeLists.txt index 3aee154aeb..bb28cfc72a 100644 --- a/tests/auto/qml/qmltc/CMakeLists.txt +++ b/tests/auto/qml/qmltc/CMakeLists.txt @@ -29,6 +29,7 @@ qt_internal_add_test(tst_qmltc_diskcache ) target_compile_definitions(tst_qmltc_diskcache PRIVATE QMLTC_TESTS_DISABLE_CACHE=0 + QT_NO_URL_CAST_FROM_STRING #QTBUG-113875 ) qt_internal_add_test(tst_qmltc_nodiskcache @@ -37,6 +38,7 @@ qt_internal_add_test(tst_qmltc_nodiskcache ) target_compile_definitions(tst_qmltc_nodiskcache PRIVATE QMLTC_TESTS_DISABLE_CACHE=1 + QT_NO_URL_CAST_FROM_STRING #QTBUG-113875 ) # Add qmltc documentation example to the tests. This is not beautiful but allows diff --git a/tests/auto/qml/qmltc/QmltcTests/CMakeLists.txt b/tests/auto/qml/qmltc/QmltcTests/CMakeLists.txt index e72d51d110..465c220c39 100644 --- a/tests/auto/qml/qmltc/QmltcTests/CMakeLists.txt +++ b/tests/auto/qml/qmltc/QmltcTests/CMakeLists.txt @@ -108,6 +108,7 @@ set(qml_sources inlineComponentsFromDifferentFiles.qml singletons.qml mySignals.qml + stringToUrl.qml # support types: DefaultPropertySingleChild.qml diff --git a/tests/auto/qml/qmltc/QmltcTests/stringToUrl.qml b/tests/auto/qml/qmltc/QmltcTests/stringToUrl.qml new file mode 100644 index 0000000000..1172c42032 --- /dev/null +++ b/tests/auto/qml/qmltc/QmltcTests/stringToUrl.qml @@ -0,0 +1,12 @@ +import QtQuick 2.12 + +QtObject { + readonly property FontLoader iconLoader: FontLoader { + source: "qrc:/qt/qml/path/to/font.ttf" + } + property string myUrl: "qrc:/qt/qml/path/to/font2.ttf" + readonly property FontLoader iconLoader2: FontLoader { + source: myUrl + } +} + diff --git a/tests/auto/qml/qmltc/tst_qmltc.cpp b/tests/auto/qml/qmltc/tst_qmltc.cpp index a4addb5308..60261d2eca 100644 --- a/tests/auto/qml/qmltc/tst_qmltc.cpp +++ b/tests/auto/qml/qmltc/tst_qmltc.cpp @@ -85,6 +85,7 @@ #include "namespacedtypes.h" #include "type.h" #include "qmltablemodel.h" +#include "stringtourl.h" // Qt: #include <QtCore/qstring.h> @@ -376,7 +377,7 @@ void tst_qmltc::properties() QCOMPARE(created.intP(), 42); QCOMPARE(created.realP(), 2.32); QCOMPARE(created.stringP(), u"hello, world"_s); - QCOMPARE(created.urlP(), u"https://www.qt.io/"_s); + QCOMPARE(created.urlP(), QUrl(u"https://www.qt.io/"_s)); QCOMPARE(created.varP(), 42.42); QCOMPARE(created.boolP(), true); @@ -3232,4 +3233,15 @@ void tst_qmltc::qmlTableModel() } #endif +void tst_qmltc::urlToString() +{ + QQmlEngine e; + PREPEND_NAMESPACE(stringToUrl) createdByQmltc(&e); + // check that the tableModel is not default constructed + QUrl first = createdByQmltc.iconLoader()->source(); + QUrl second = createdByQmltc.iconLoader2()->source(); + QCOMPARE(first, QUrl("qrc:/qt/qml/path/to/font.ttf")); + QCOMPARE(second, QUrl("qrc:/qt/qml/path/to/font2.ttf")); +} + QTEST_MAIN(tst_qmltc) diff --git a/tests/auto/qml/qmltc/tst_qmltc.h b/tests/auto/qml/qmltc/tst_qmltc.h index a2a0d16fd3..628072ecc3 100644 --- a/tests/auto/qml/qmltc/tst_qmltc.h +++ b/tests/auto/qml/qmltc/tst_qmltc.h @@ -99,4 +99,5 @@ private slots: #if QT_CONFIG(qml_table_model) void qmlTableModel(); #endif + void urlToString(); }; diff --git a/tools/qmltc/qmltccompiler.cpp b/tools/qmltc/qmltccompiler.cpp index 61205cef55..6e8430c3c8 100644 --- a/tools/qmltc/qmltccompiler.cpp +++ b/tools/qmltc/qmltccompiler.cpp @@ -1506,7 +1506,13 @@ void QmltcCompiler::compileBindingByType(QmltcType ¤t, break; } case QQmlSA::BindingType::StringLiteral: { - assignToProperty(metaProperty, QQmlJSUtils::toLiteral(binding.stringValue())); + QString value = QQmlJSUtils::toLiteral(binding.stringValue()); + if (auto type = metaProperty.type()) { + if (type->internalName() == u"QUrl"_s) { + value = u"QUrl("_s.append(std::move(value)).append(u")"_s); + } + } + assignToProperty(metaProperty, value); break; } case QQmlSA::BindingType::RegExpLiteral: { |