diff options
author | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2024-04-29 12:03:44 +0200 |
---|---|---|
committer | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2024-04-30 10:50:25 +0200 |
commit | 336d799755bd574e481f919eeacd70b780967ef3 (patch) | |
tree | e865d2926d55d3ba0e15b97028f5b603ccd2733b | |
parent | be74f16fcf429916e261f6e995cf75bdfa8b8960 (diff) |
PySide: Fix QML ListProperty
- fix broken propListCount() by changing the type of the returned value
to qsizetype. Amends f9662f9b4008f683d02c829ea59676a2cf49b2fb.
- Add tests to test all the ListProperty features.
- Fixes some flake8 errors.
Pick-to: 6.2 6.5 6.6 6.7
Task-number: PYSIDE-2698
Change-Id: I356e8af3af965374441f287391dcab9d724b28f9
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
-rw-r--r-- | sources/pyside6/libpysideqml/pysideqmllistproperty.cpp | 2 | ||||
-rw-r--r-- | sources/pyside6/tests/QtQml/listproperty.py | 87 | ||||
-rw-r--r-- | sources/pyside6/tests/QtQml/listproperty.qml | 50 |
3 files changed, 129 insertions, 10 deletions
diff --git a/sources/pyside6/libpysideqml/pysideqmllistproperty.cpp b/sources/pyside6/libpysideqml/pysideqmllistproperty.cpp index 97d6ce91b..75bb5af96 100644 --- a/sources/pyside6/libpysideqml/pysideqmllistproperty.cpp +++ b/sources/pyside6/libpysideqml/pysideqmllistproperty.cpp @@ -169,7 +169,7 @@ qsizetype propListCount(QQmlListProperty<QObject> *propList) return 0; } - int cppResult = 0; + qsizetype cppResult = 0; auto *converter = Shiboken::Conversions::PrimitiveTypeConverter<qsizetype>(); if (auto *pythonToCpp = Shiboken::Conversions::isPythonToCppConvertible(converter, retVal)) pythonToCpp(retVal, &cppResult); diff --git a/sources/pyside6/tests/QtQml/listproperty.py b/sources/pyside6/tests/QtQml/listproperty.py index 8916aefe5..884600d29 100644 --- a/sources/pyside6/tests/QtQml/listproperty.py +++ b/sources/pyside6/tests/QtQml/listproperty.py @@ -7,11 +7,25 @@ import unittest from pathlib import Path sys.path.append(os.fspath(Path(__file__).resolve().parents[1])) -from init_paths import init_test_paths +from init_paths import init_test_paths # noqa: E402 init_test_paths(False) -from PySide6.QtCore import QObject -from PySide6.QtQml import ListProperty +from helper.usesqapplication import UsesQApplication # noqa: E402, F401 + +from PySide6.QtCore import QObject, QUrl, Property, qInstallMessageHandler # noqa: E402 +from PySide6.QtQml import ListProperty, QmlElement # noqa: E402 +from PySide6.QtQuick import QQuickView # noqa: E402 + + +QML_IMPORT_NAME = "test.ListPropertyTest" +QML_IMPORT_MAJOR_VERSION = 1 + +output_messages = [] + + +def message_handler(mode, context, message): + global output_messages + output_messages.append(f"{message}") class InheritsQObject(QObject): @@ -22,7 +36,46 @@ def dummyFunc(): pass -class TestListProperty(unittest.TestCase): +@QmlElement +class Person(QObject): + def __init__(self, parent=None): + super().__init__(parent=None) + self._name = '' + self._friends = [] + + def appendFriend(self, friend): + self._friends.append(friend) + + def friendCount(self): + return len(self._friends) + + def friend(self, index): + return self._friends[index] + + def removeLastItem(self): + if len(self._friends) > 0: + self._friends.pop() + + def replace(self, index, friend): + if 0 <= index < len(self._friends): + self._friends[index] = friend + + def clear(self): + self._friends.clear() + + @Property(str, final=True) + def name(self): + return self._name + + @name.setter + def name(self, value): + self._name = value + + friends = ListProperty(QObject, append=appendFriend, count=friendCount, at=friend, + removeLast=removeLastItem, replace=replace, clear=clear) + + +class TestListProperty(UsesQApplication): def testIt(self): # Verify that type checking works properly @@ -31,7 +84,7 @@ class TestListProperty(unittest.TestCase): try: ListProperty(QObject) ListProperty(InheritsQObject) - except: + except Exception: type_check_error = True self.assertFalse(type_check_error) @@ -47,21 +100,37 @@ class TestListProperty(unittest.TestCase): method_check_error = False try: - ListProperty(QObject, append=None, at=None, count=None, replace=None, clear=None, removeLast=None) # Explicitly setting None + ListProperty(QObject, append=None, at=None, count=None, replace=None, clear=None, + removeLast=None) # Explicitly setting None ListProperty(QObject, append=dummyFunc) ListProperty(QObject, count=dummyFunc, at=dummyFunc) - except: + except Exception: method_check_error = True self.assertFalse(method_check_error) try: - ListPropery(QObject, append=QObject()) - except: + ListProperty(QObject, append=QObject()) + except Exception: method_check_error = True self.assertTrue(method_check_error) + def testListPropParameters(self): + global output_messages + qInstallMessageHandler(message_handler) + view = QQuickView() + file = Path(__file__).resolve().parent / 'listproperty.qml' + self.assertTrue(file.is_file()) + view.setSource(QUrl.fromLocalFile(file)) + view.show() + self.assertEqual(output_messages[0], "List length: 3") + self.assertEqual(output_messages[1], "First element: Alice") + self.assertEqual(output_messages[2], "Removing last item: Charlie") + self.assertEqual(output_messages[3], "Replacing last item: Bob") + self.assertEqual(output_messages[4], "Replaced last item: David") + self.assertEqual(output_messages[5], "List length after clearing: 0") + if __name__ == '__main__': unittest.main() diff --git a/sources/pyside6/tests/QtQml/listproperty.qml b/sources/pyside6/tests/QtQml/listproperty.qml new file mode 100644 index 000000000..7b71e30ba --- /dev/null +++ b/sources/pyside6/tests/QtQml/listproperty.qml @@ -0,0 +1,50 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick 2.0 +import test.ListPropertyTest + +Rectangle { + width: 360 + height: 360 + + Person { + id: person + friends: [ + Person{ + name: "Alice" + }, + Person{ + name: "Bob" + }, + Person{ + name: "Charlie" + } + ] + } + + Person{ + id: david + name: "David" + } + + Component.onCompleted: { + // Access the length of the list + console.log("List length: " + person.friends.length); + + // Access the first element of the list + console.log("First element: " + person.friends[0].name); + + // Remove the last item of the list + console.log("Removing last item: " + person.friends.pop().name); + + // Repalce the last item of the list + console.log("Replacing last item: " + person.friends[person.friends.length - 1].name); + person.friends[person.friends.length - 1] = david; + console.log("Replaced last item: " + person.friends[person.friends.length - 1].name); + + // Clear the list + person.friends = []; + console.log("List length after clearing: " + person.friends.length); + } +} |