[Kde-bindings] branches/KDE/4.4/kdebindings/csharp/qyoto/core

Arno Rehn kde at arnorehn.de
Wed Feb 10 22:41:57 UTC 2010


SVN commit 1088476 by arnorehn:

Fix handling of non-Qt types in QVariant.

CCMAIL: kde-bindings at kde.org


 M  +17 -2     QVariantExtras.cs  


--- branches/KDE/4.4/kdebindings/csharp/qyoto/core/QVariantExtras.cs #1088475:1088476
@@ -78,15 +78,22 @@
                     typeName = SmokeMarshallers.SmokeClassName(typeof(T));
                 else
                     typeName = typeof(T).ToString();
-                if (NameToType(typeName) > TypeOf.LastCoreType) {
+                TypeOf type = NameToType(typeName);
+                if (type > TypeOf.LastCoreType) {
                     IntPtr instancePtr = QVariantValue(typeName, (IntPtr) GCHandle.Alloc(this));
                     return (T) ((GCHandle) instancePtr).Target;
+                } else if (type == TypeOf.Invalid) {
+                    Console.WriteLine("QVariant.Value<{0}>(): invalid type", typeof(T));
                 }
 
                 return (T) (object) default(T);
             }
         }
 
+        static public QVariant FromValue<T>(T value) {
+            return FromValue<T>((object) value);
+        }
+
         static public QVariant FromValue<T>(object value) {
             if (typeof(T) == typeof(bool)) {
                 return new QVariant((bool) value);
@@ -148,11 +155,19 @@
                     typeName = SmokeMarshallers.SmokeClassName(typeof(T));
                 else
                     typeName = typeof(T).ToString();
-                if (NameToType(typeName) > TypeOf.LastCoreType) {
+                TypeOf type = NameToType(typeName);
+                if (type == TypeOf.Invalid) {
+                    // user type, not yet registered
+                    QMetaType.RegisterType<T>();
+                    type = NameToType(typeName);
+                }
+                if (type > TypeOf.LastCoreType) {
                     GCHandle handle = (GCHandle) QVariantFromValue(QMetaType.type(typeName), (IntPtr) GCHandle.Alloc(value));
                     QVariant v = (QVariant) handle.Target;
                     handle.Free();
                     return v;
+                } else if (type == TypeOf.Invalid) {
+                    throw new Exception("Failed to register type " + typeof(T).ToString());
                 }
 
                 return new QVariant();



More information about the Kde-bindings mailing list