From 308aba2154699596e3fd0973501213423462a80b Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 24 Mar 2020 12:03:50 +0100 Subject: PySide2: Use int for QVariant conversion when possible Check using the init limits in the QVariant long long conversion and create an int if possible. This works more smoothly with Qt and for example ensures that the correct editor is created in item views. Change-Id: I0ca2e5e7b91f309deaa81a25e70a5f894f43f841 Fixes: PYSIDE-1250 Reviewed-by: Cristian Maureira-Fredes --- sources/pyside2/PySide2/glue/qtcore.cpp | 6 +++++- sources/pyside2/tests/QtCore/qsettings_test.py | 5 +---- .../tests/pysidetest/delegatecreateseditor_test.py | 18 +++++++++++++++++- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/sources/pyside2/PySide2/glue/qtcore.cpp b/sources/pyside2/PySide2/glue/qtcore.cpp index b870afa55..60ddf675c 100644 --- a/sources/pyside2/PySide2/glue/qtcore.cpp +++ b/sources/pyside2/PySide2/glue/qtcore.cpp @@ -43,6 +43,7 @@ // @snippet include-pyside #include +#include // @snippet include-pyside // @snippet pystring-check @@ -1716,8 +1717,11 @@ int i = %CONVERTTOCPP[int](%in); // @snippet conversion-pyint // @snippet conversion-qlonglong +// PYSIDE-1250: For QVariant, if the type fits into an int; use int preferably. qlonglong in = %CONVERTTOCPP[qlonglong](%in); -%out = %OUTTYPE(in); +constexpr qlonglong intMax = qint64(std::numeric_limits::max()); +constexpr qlonglong intMin = qint64(std::numeric_limits::min()); +%out = in >= intMin && in <= intMax ? %OUTTYPE(int(in)) : %OUTTYPE(in); // @snippet conversion-qlonglong // @snippet conversion-qstring diff --git a/sources/pyside2/tests/QtCore/qsettings_test.py b/sources/pyside2/tests/QtCore/qsettings_test.py index 982ddc38d..639f6d276 100644 --- a/sources/pyside2/tests/QtCore/qsettings_test.py +++ b/sources/pyside2/tests/QtCore/qsettings_test.py @@ -75,10 +75,7 @@ class TestQSettings(unittest.TestCase): # Handling zero value r = settings.value('zero_value') - if py3k.IS_PY3K: - self.assertEqual(type(r), int) - else: - self.assertEqual(type(r), long) + self.assertEqual(type(r), int) r = settings.value('zero_value', type=int) self.assertEqual(type(r), int) diff --git a/sources/pyside2/tests/pysidetest/delegatecreateseditor_test.py b/sources/pyside2/tests/pysidetest/delegatecreateseditor_test.py index 1378ca8ad..111d4ebbf 100644 --- a/sources/pyside2/tests/pysidetest/delegatecreateseditor_test.py +++ b/sources/pyside2/tests/pysidetest/delegatecreateseditor_test.py @@ -39,7 +39,10 @@ init_test_paths(True) from helper.usesqapplication import UsesQApplication from testbinding import TestView from PySide2.QtCore import Qt -from PySide2.QtWidgets import QAbstractItemDelegate, QComboBox +from PySide2.QtGui import QStandardItem, QStandardItemModel +from PySide2.QtWidgets import (QAbstractItemDelegate, QComboBox, + QSpinBox, QStyledItemDelegate, + QStyleOptionViewItem, QWidget) id_text = 'This is me' @@ -83,6 +86,19 @@ class EditorCreatedByDelegateTest(UsesQApplication): self.assertEqual(editor.itemData(0, Qt.DisplayRole), id_text) editor.metaObject() + def testIntDelegate(self): + """PYSIDE-1250: When creating a QVariant, use int instead of long long + for anything that fits into a int. Verify by checking that a spin + box is created as item view editor for int.""" + item = QStandardItem() + item.setData(123123, Qt.EditRole) # <-- QVariant conversion here + model = QStandardItemModel() + model.appendRow(item) + style_option = QStyleOptionViewItem() + delegate = QStyledItemDelegate() + editor = delegate.createEditor(None, style_option, model.index(0, 0)) + self.assertEqual(type(editor), QSpinBox) + if __name__ == '__main__': unittest.main() -- cgit v1.2.3