[PyQt] Using QVariant.toPyObject to wrap the to* functions
Henning Schröder
henning.schroeder at gmail.com
Thu Aug 14 20:19:28 BST 2008
On Thu, Aug 14, 2008 at 9:44 AM, Arve Knudsen <arve.knudsen at gmail.com> wrote:
> On Wed, Aug 13, 2008 at 9:37 PM, Erick Tryzelaar
> <idadesub at users.sourceforge.net> wrote:
>> [..]
>> and etc. This makes QVariant a little more pythonic to me, in my opinion.
>
> I agree about this. It would be more natural to have toPyObject work
> for the standard types in addition to custom ones, and simplify the
> logic greatly.
Yes this would be a great improvement.
I currently use the function below but having this available via
toPyObject() would be a great improvement.
variant_converter = {
"QVariantList": lambda v: from_variantlist(v),
"QList<QVariant>": lambda v: from_variantlist(v),
"int": lambda v: v.toInt()[0],
"double": lambda v: v.toDouble()[0],
"char": lambda v: v.toChar(),
"QByteArray": lambda v: v.toByteArray(),
"QString": lambda v: unicode(v.toString()),
"QPoint": lambda v: v.toPoint(),
"QPointF": lambda v: v.toPointF(),
"QSize": lambda v: v.toSize(),
"QLine": lambda v: v.toLine(),
"QStringList": lambda v: v.toStringList(),
"QTime": lambda v: v.toTime(),
"QDateTime": lambda v: v.toDateTime(),
"QDate": lambda v: v.toDate(),
"QLocale": lambda v: v.toLocale(),
"QUrl": lambda v: v.toUrl(),
"QRect": lambda v: v.toRect(),
"QBrush": lambda v: QBrush(v),
"QFont": lambda v: QFont(v),
"QPalette": lambda v: QPalette(v),
"QPixmap": lambda v: QPixmap(v),
"QImage": lambda v: QImage(v),
"bool": lambda v: v.toBool(),
"QObject*": lambda v: ScripterNG.fromVariant(v),
"QWidget*": lambda v: ScripterNG.fromVariant(v),
}
def from_variantlist(variantlist):
"""
convert QList<QVariant> to a normal Python list
"""
return [from_variant(variant) for variant in variantlist.toList()]
def from_variant(variant):
"""
convert a QVariant to a Python value
"""
typeName = variant.typeName()
convert = variant_converter.get(typeName)
if not convert:
raise ValueError, "Could not convert value to %s" % typeName
else:
return convert(variant)
More information about the PyQt
mailing list