Problem with Gui::InputField property "quantity" using python (and possible locale issue)

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
lorenz
Posts: 30
Joined: Wed Mar 16, 2016 9:35 pm

Problem with Gui::InputField property "quantity" using python (and possible locale issue)

Postby lorenz » Sat Jan 14, 2017 7:46 pm

Hi there,

I am having trouble using the nice unit-aware Gui::InputField widget from python. I can query the "quantity" property just fine:

Code: Select all

>>> widget = ui.createWidget("Gui::InputField")
>>> widget.setText("1 mm")
>>> q = widget.property("quantity")
>>> q
1 mm
However, it does not seem possible to set the attribute (even though it is declared as writable and should accept a Base::Quantity object:

Code: Select all

>>> type(q)
<type 'Base.Quantity'>
>>> widget.setProperty("quantity", q)
False
The background for this is that using "setText()" apparently has some locale issues:

Code: Select all

>>> widget.setText("1.5 mm")
>>> widget.property("quantity")
15 mm
>>> widget.setText("1,5 mm")
>>> widget.property("quantity")
1.5 mm
I am not sure how the properly format the string, as the current locale settings seem to suggest that "1.5" is correct:

Code: Select all

>>> import locale
>>> locale.str(1.5)
'1.5'
So there might be another issue lurking there. In any case, I would much prefer to set the quantity without first formatting it as a string and then parsing it again at the C++ side :)

Kind regards,
Lorenz
chrisb
Posts: 19589
Joined: Tue Mar 17, 2015 9:14 am

Re: Problem with Gui::InputField property "quantity" using python (and possible locale issue)

Postby chrisb » Sat Jan 14, 2017 11:07 pm

Please have a look here: viewtopic.php?f=10&t=18833&start=10. Or if you need an example: in FreeCADDaily in Mod/Path/PathScripts/PathPocket.py in the functions setFields and getFields.
wmayer
Site Admin
Posts: 14992
Joined: Thu Feb 19, 2009 10:32 am

Re: Problem with Gui::InputField property "quantity" using python (and possible locale issue)

Postby wmayer » Sun Jan 15, 2017 11:16 am

However, it does not seem possible to set the attribute (even though it is declared as writeable and should accept a Base::Quantity object:
Actually this is supposed to work too but in fact it doesn't. To find the reason of the failure I debugged through the code and here are my findings:
The function Sbk_QObjectFunc_setProperty is the Python wrapper for QObject::setProperty and for the Python wrapper of Base::Quantity it falls back to a conversion function that creates a QVariant from a PySide::PyObjectWrapper but actually expected is a QVariant from a Base::Quantity.

Now when trying to assign the QVariant of PySide::PyObjectWrapper Qt's meta type system searches for a conversion function that converts between PySide::PyObjectWrapper and Base::Quantity but it couldn't find one and thus can't continue.

I think there are two ways to solve the problem:
1. Offer a conversion function to PySide to avoid to fall back to the generic PyObjectWrapper
2. If this is not possible then offer a conversion function in Qt to convert between PyObjectWrapper and Base::Quantity
wmayer
Site Admin
Posts: 14992
Joined: Thu Feb 19, 2009 10:32 am

Re: Problem with Gui::InputField property "quantity" using python (and possible locale issue)

Postby wmayer » Sun Jan 15, 2017 2:28 pm

It's quite easy to implement a shiboken converter function from QuantityPy to QVariant. Unfortunately, this converter can only be added to the end so that it's pretty useless. At least the second way works in Qt to write a converter between two meta types. However, this is only possible with Qt5.2.

So, for Qt4 there is no solution but for Qt >= 5.2 there is one.

git commit 2f66ff6