Add repr for QgsMargins

This commit is contained in:
Nyall Dawson 2024-10-29 14:40:06 +10:00
parent 3f795c544d
commit e9a51d6806
4 changed files with 24 additions and 0 deletions

View File

@ -124,6 +124,12 @@ if the string could not be interpreted as margins.
.. seealso:: :py:func:`toString`
%End
SIP_PYOBJECT __repr__();
%MethodCode
const QString str = QStringLiteral( "<QgsMargins: %1 %2 %3 %4>" ).arg( sipCpp->left() ).arg( sipCpp->top() ).arg( sipCpp->right() ).arg( sipCpp->bottom() );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End
};

View File

@ -124,6 +124,12 @@ if the string could not be interpreted as margins.
.. seealso:: :py:func:`toString`
%End
SIP_PYOBJECT __repr__();
%MethodCode
const QString str = QStringLiteral( "<QgsMargins: %1 %2 %3 %4>" ).arg( sipCpp->left() ).arg( sipCpp->top() ).arg( sipCpp->right() ).arg( sipCpp->bottom() );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End
};

View File

@ -160,6 +160,14 @@ class CORE_EXPORT QgsMargins
*/
static QgsMargins fromString( const QString &string );
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
const QString str = QStringLiteral( "<QgsMargins: %1 %2 %3 %4>" ).arg( sipCpp->left() ).arg( sipCpp->top() ).arg( sipCpp->right() ).arg( sipCpp->bottom() );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif
private:
double mLeft = 0.0;
double mTop = 0.0;

View File

@ -34,6 +34,10 @@ class TestQgsOptional(unittest.TestCase):
self.assertFalse(margins.isNull())
self.assertEqual(margins, QgsMargins(5.5, 0.0, 5.5, 0.0))
def test_repr(self):
self.assertEqual(str(QgsMargins(12.1, 14.1, 16.1, 18.1)),
'<QgsMargins: 12.1 14.1 16.1 18.1>')
def testOperators(self):
m1 = QgsMargins(12.1, 14.1, 16.1, 18.1)
m2 = QgsMargins(2.1, 3.1, 4.1, 5.1)