aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2024-09-09 14:07:57 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2024-09-11 11:21:16 +0200
commitba83ea75449f7e86525adcc796ccce800e86fb92 (patch)
tree439ceaaf1ba59021029b01e01bdcbbec3b7c0573 /sources/pyside6
parent8b08cebf577964e164c9fe2fa776e18d53cfe8d6 (diff)
PySide6: Add QModelRoleDataSpan
Implement QModelRoleDataSpan with an index operator returning QModelRoleData* for modification. This enables QAbstractItemModel::multiData(). [ChangeLog][PySide6] QModelRoleDataSpan for implementing QAbstractItemModel.multiData() has been added. Fixes: PYSIDE-2859 Change-Id: I35e4812bd999538f6dbd9e63a623791640287bab Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources/pyside6')
-rw-r--r--sources/pyside6/PySide6/QtCore/CMakeLists.txt1
-rw-r--r--sources/pyside6/PySide6/QtCore/typesystem_core_common.xml19
-rw-r--r--sources/pyside6/PySide6/glue/qtcore.cpp20
-rw-r--r--sources/pyside6/tests/QtCore/qabstractitemmodel_test.py22
4 files changed, 57 insertions, 5 deletions
diff --git a/sources/pyside6/PySide6/QtCore/CMakeLists.txt b/sources/pyside6/PySide6/QtCore/CMakeLists.txt
index 369e81865..d985b9778 100644
--- a/sources/pyside6/PySide6/QtCore/CMakeLists.txt
+++ b/sources/pyside6/PySide6/QtCore/CMakeLists.txt
@@ -118,6 +118,7 @@ ${QtCore_GEN_DIR}/qmimedatabase_wrapper.cpp
${QtCore_GEN_DIR}/qmimetype_wrapper.cpp
${QtCore_GEN_DIR}/qmodelindex_wrapper.cpp
${QtCore_GEN_DIR}/qmodelroledata_wrapper.cpp
+${QtCore_GEN_DIR}/qmodelroledataspan_wrapper.cpp
${QtCore_GEN_DIR}/qmutex_wrapper.cpp
${QtCore_GEN_DIR}/qnativeipckey_wrapper.cpp
${QtCore_GEN_DIR}/qobject_wrapper.cpp
diff --git a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml
index f93f1b49e..c830bf852 100644
--- a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml
+++ b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml
@@ -1643,7 +1643,24 @@
<modify-function signature="exit(int)" allow-thread="yes"/>
</object-type>
- <value-type name="QModelRoleData"/>
+ <value-type name="QModelRoleData">
+ <add-function signature="setData(const QVariant &amp;@data@)">
+ <inject-code class="target" position="beginning" file="../glue/qtcore.cpp"
+ snippet="qmodelroledata-setdata"/>
+ </add-function>
+ </value-type>
+
+ <value-type name="QModelRoleDataSpan">
+ <modify-function signature="dataForRole(int)const" remove="all"/>
+ <add-function signature="__len__">
+ <inject-code class="target" position="beginning" file="../glue/qtcore.cpp"
+ snippet="qmodelroledataspan-len"/>
+ </add-function>
+ <add-function signature="__getitem__">
+ <inject-code class="target" position="beginning" file="../glue/qtcore.cpp"
+ snippet="qmodelroledataspan-getitem"/>
+ </add-function>
+ </value-type>
<!-- Register meta type for QML properties -->
<object-type name="QAbstractItemModel" qt-register-metatype="yes">
diff --git a/sources/pyside6/PySide6/glue/qtcore.cpp b/sources/pyside6/PySide6/glue/qtcore.cpp
index 4f0556d54..56d813b7e 100644
--- a/sources/pyside6/PySide6/glue/qtcore.cpp
+++ b/sources/pyside6/PySide6/glue/qtcore.cpp
@@ -565,6 +565,26 @@ Py_XDECREF(result);
return !result ? -1 : 0;
// @snippet qbitarray-setitem
+// @snippet qmodelroledata-setdata
+// Call template <typename T> void QModelRoleData::setData(T &&value)
+%CPPSELF.%FUNCTION_NAME(%1);
+// @snippet qmodelroledata-setdata
+
+// @snippet qmodelroledataspan-len
+return %CPPSELF.size();
+// @snippet qmodelroledataspan-len
+
+// @snippet qmodelroledataspan-getitem
+const Py_ssize_t size = %CPPSELF.size();
+if (_i < 0 || _i >= size) {
+ Shiboken::Errors::setIndexOutOfBounds(_i, 0, size);
+ return nullptr;
+}
+// Return a pointer to allow for modification using QModelRoleData::setData()
+QModelRoleData *item = &((*%CPPSELF)[_i]);
+return %CONVERTTOPYTHON[QModelRoleData *](item);
+// @snippet qmodelroledataspan-getitem
+
// @snippet default-enter
Py_INCREF(%PYSELF);
pyResult = %PYSELF;
diff --git a/sources/pyside6/tests/QtCore/qabstractitemmodel_test.py b/sources/pyside6/tests/QtCore/qabstractitemmodel_test.py
index 20d070a03..f1b0f8b0b 100644
--- a/sources/pyside6/tests/QtCore/qabstractitemmodel_test.py
+++ b/sources/pyside6/tests/QtCore/qabstractitemmodel_test.py
@@ -20,15 +20,25 @@ class MyModel (QAbstractListModel):
pass
+class MyMultiDataModel(QAbstractListModel):
+
+ def multiData(self, index, roleSpans):
+ if index.isValid():
+ for rd in roleSpans:
+ if rd.role() == Qt.ItemDataRole.DisplayRole:
+ rd.setData(f"test {index.row()} {index.column()}")
+
+
class TestQModelIndexInternalPointer(unittest.TestCase):
def testInternalPointer(self):
m = MyModel()
foo = QObject()
idx = m.createIndex(0, 0, foo)
- check = m.checkIndex(idx, QAbstractItemModel.CheckIndexOption.IndexIsValid
- | QAbstractItemModel.CheckIndexOption.DoNotUseParent
- | QAbstractItemModel.CheckIndexOption.ParentIsInvalid)
+ flags = (QAbstractItemModel.CheckIndexOption.IndexIsValid
+ | QAbstractItemModel.CheckIndexOption.DoNotUseParent
+ | QAbstractItemModel.CheckIndexOption.ParentIsInvalid)
+ check = m.checkIndex(idx, flags)
self.assertTrue(check)
def testPassQPersistentModelIndexAsQModelIndex(self):
@@ -47,7 +57,11 @@ class TestQModelIndexInternalPointer(unittest.TestCase):
proxyData = str(proxyModel.data(proxyIndex, Qt.DisplayRole))
self.assertEqual(sourceData, proxyData)
+ def testMultiDataModel(self):
+ """Test whether QAbstractItemModel.multiData() can be implemented
+ using QModelRoleData/QModelRoleDataSpan (ATM syntax only)."""
+ model = MyMultiDataModel() # noqa: F841
+
if __name__ == '__main__':
unittest.main()
-