diff options
| -rw-r--r-- | sources/pyside6/tests/QtWidgets/qlayout_test.py | 7 | ||||
| -rw-r--r-- | sources/shiboken6/generator/shiboken/cppgenerator.cpp | 30 | ||||
| -rw-r--r-- | sources/shiboken6/generator/shiboken/cppgenerator.h | 4 |
3 files changed, 35 insertions, 6 deletions
diff --git a/sources/pyside6/tests/QtWidgets/qlayout_test.py b/sources/pyside6/tests/QtWidgets/qlayout_test.py index 72eb586a1..cc41f78b1 100644 --- a/sources/pyside6/tests/QtWidgets/qlayout_test.py +++ b/sources/pyside6/tests/QtWidgets/qlayout_test.py @@ -127,6 +127,13 @@ class QLayoutTest(UsesQApplication): gc.collect() self.assertRaises(RuntimeError, spacer.isEmpty) + def testConstructorProperties(self): + """PYSIDE-1986, test passing properties to the constructor of + QHBoxLayout, which does not have default arguments.""" + layout = QHBoxLayout(objectName="layout", spacing=30) + self.assertEqual(layout.spacing(), 30) + self.assertEqual(layout.objectName(), "layout") + if __name__ == '__main__': unittest.main() diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index 1878dd4d9..99cac73d0 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -3595,11 +3595,32 @@ void CppGenerator::writeSetPythonToCppPointerConversion(TextStream &s, converterVar, pythonToCppFunc, isConvertibleFunc); } +// PYSIDE-1986: Some QObject derived classes, (QVBoxLayout) do not have default +// arguments, which breaks setting properties by named arguments. Force the +// handling code to be generated nevertheless for applicable widget classes, +// so that the mechanism of falling through to the error handling to set +// the properties works nevertheless. +static bool forceQObjectNamedArguments(const AbstractMetaFunctionCPtr &func) +{ + if (func->functionType() != AbstractMetaFunction::ConstructorFunction) + return false; + auto *owner = func->ownerClass(); + Q_ASSERT(owner); + if (!owner->isQObject()) + return false; + const QString &name = owner->name(); + return name == u"QVBoxLayout" || name == u"QHBoxLayout" + || name == u"QSplitterHandle" || name == u"QSizeGrip"; +} + void CppGenerator::writeNamedArgumentResolution(TextStream &s, const AbstractMetaFunctionCPtr &func, - bool usePyArgs, const OverloadData &overloadData) + bool usePyArgs, const OverloadData &overloadData) const { const AbstractMetaArgumentList &args = OverloadData::getArgumentsWithDefaultValues(func); - if (args.isEmpty()) { + const bool hasDefaultArguments = !args.isEmpty(); + const bool force = !hasDefaultArguments && usePySideExtensions() + && forceQObjectNamedArguments(func); + if (!hasDefaultArguments && !force) { if (overloadData.hasArgumentWithDefaultValue()) { // PySide-535: Allow for empty dict instead of nullptr in PyPy s << "if (kwds && PyDict_Size(kwds) > 0) {\n"; @@ -3618,8 +3639,9 @@ void CppGenerator::writeNamedArgumentResolution(TextStream &s, const AbstractMet s << "if (kwds && PyDict_Size(kwds) > 0) {\n"; { Indentation indent(s); - s << "PyObject *value{};\n" - << "Shiboken::AutoDecRef kwds_dup(PyDict_Copy(kwds));\n"; + if (!force) + s << "PyObject *value{};\n"; + s << "Shiboken::AutoDecRef kwds_dup(PyDict_Copy(kwds));\n"; for (const AbstractMetaArgument &arg : args) { const int pyArgIndex = arg.argumentIndex() - OverloadData::numberOfRemovedArguments(func, arg.argumentIndex()); diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.h b/sources/shiboken6/generator/shiboken/cppgenerator.h index a989fe35f..4d7f4e7c2 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.h +++ b/sources/shiboken6/generator/shiboken/cppgenerator.h @@ -331,8 +331,8 @@ private: const QString &pythonToCppFunc, const QString &isConvertibleFunc); - static void writeNamedArgumentResolution(TextStream &s, const AbstractMetaFunctionCPtr &func, - bool usePyArgs, const OverloadData &overloadData); + void writeNamedArgumentResolution(TextStream &s, const AbstractMetaFunctionCPtr &func, + bool usePyArgs, const OverloadData &overloadData) const; /// Returns a string containing the name of an argument for the given function and argument index. static QString argumentNameFromIndex(const ApiExtractorResult &api, |
