Logo Search packages:      
Sourcecode: qtjambi version File versions  Download package

com::trolltech::qt::QVariant Class Reference

Inheritance diagram for com::trolltech::qt::QVariant:

com::trolltech::qt::QtJambiObject com::trolltech::qt::QSignalEmitter com::trolltech::qt::QtJambiInterface

List of all members.


Detailed Description

The QVariant class acts like a union for the most common Qt data types.

Because C++ forbids unions from including types that have non-default constructors or destructors, most interesting Qt classes cannot be used in unions. Without QVariant, this would be a problem for QObject::property() and for database work, etc.

A QVariant object holds a single value of a single type() at a time. (Some type()s are multi-valued, for example a string list.) You can find out what type, T, the variant holds, convert it to a different type using convert(), get its value using one of the toT() functions (e.g., toSize()) and check whether the type can be converted to a particular type using canConvert().

The methods named toT() (e.g., toInt(), toString()) are const. If you ask for the stored type, they return a copy of the stored object. If you ask for a type that can be generated from the stored type, toT() copies and converts and leaves the object itself unchanged. If you ask for a type that cannot be generated from the stored type, the result depends on the type; see the function documentation for details.

Here is some example code to demonstrate the use of QVariant:

    QDataStream out(...);
    QVariant v(123);                // The variant now contains an int
    int x = v.toInt();              // x = 123
    out << v;                       // Writes a type tag and an int to out
    v = QVariant("hello");          // The variant now contains a QByteArray
    v = QVariant(tr("hello"));      // The variant now contains a QString
    int y = v.toInt();              // y = 0 since v cannot be converted to an int
    QString s = v.toString();       // s = tr("hello")  (see QObject::tr())
    out <<v;                  // Writes a type tag and a QString to out
    ...
    QDataStream in(...);            // (opening the previously written stream)
    in >> v;                  // Reads an Int variant
    int z = v.toInt();              // z = 123
    qDebug("Type is %s",            // prints "Type is int"
            v.typeName());
    v = v.toInt() + 100;            // The variant now hold the value 223
    v = QVariant(QStringList());

You can even store QList<QVariant> and QMap<QString, QVariant> values in a variant, so you can easily construct arbitrarily complex data structures of arbitrary types. This is very powerful and versatile, but may prove less memory and speed efficient than storing specific types in standard data structures.

QVariant also supports the notion of null values, where you have a defined type with no value set.

    QVariant x, y(QString()), z(QString(""));
    x.convert(QVariant::Int);
    // x.isNull() == true
    // y.isNull() == true, z.isNull() == false
    // y.isEmpty() == true, z.isEmpty() == true
</code

QVariant can be extended to support other types than those mentioned in the Type enum. See the QMetaType documentation for details.

A Note on GUI Types

Because QVariant is part of the QtCore library, it cannot provide conversion functions to data types defined in QtGui, such as QColor, QImage, and QPixmap. In other words, there is no toColor() function. Instead, you can use the QVariant::value() or the qVariantValue() template function. For example:

    QVariant variant;
    ...
    QColor color = variant.value<QColor>();

The inverse conversion (e.g., from QColor to QVariant) is automatic for all data types supported by QVariant, including GUI-related types:

    QColor color = palette().background().color();
    QVariant variant = color;

Because of Java's introspection, you should only use the QVariant class when dealing with Qt Jambi classes that requires them.

Definition at line 135 of file QVariant.java.


Public Member Functions

boolean blockSignals (boolean b)
final native void disableGarbageCollection ()
final void disconnect (Object other)
final void disconnect ()
final native void dispose ()
final long nativeId ()
final native QNativePointer nativePointer ()
final native void reenableGarbageCollection ()
final native void setJavaOwnership ()
boolean signalsBlocked ()
Thread thread ()
String tr (String source, String comment, int count)
String tr (String source, String comment)
String tr (String source)

Static Public Member Functions

static boolean canConvertToBitArray (Object obj)
static boolean canConvertToBoolean (Object obj)
static boolean canConvertToByteArray (Object obj)
static boolean canConvertToChar (Object obj)
static boolean canConvertToDate (Object obj)
static boolean canConvertToDateTime (Object obj)
static boolean canConvertToDouble (Object obj)
static boolean canConvertToInt (Object obj)
static boolean canConvertToLine (Object obj)
static boolean canConvertToLineF (Object obj)
static boolean canConvertToList (Object obj)
static boolean canConvertToLocale (Object obj)
static boolean canConvertToLong (Object obj)
static boolean canConvertToMap (Object obj)
static boolean canConvertToPoint (Object obj)
static boolean canConvertToPointF (Object obj)
static boolean canConvertToRect (Object obj)
static boolean canConvertToRectF (Object obj)
static boolean canConvertToRegExp (Object obj)
static boolean canConvertToSize (Object obj)
static boolean canConvertToSizeF (Object obj)
static boolean canConvertToString (Object obj)
static boolean canConvertToTime (Object obj)
static QtJambiObject reassignNativeResources (QtJambiObject object, Class<?extends QtJambiObject > clazz) throws InstantiationException
static QSignalEmitter signalSender ()
static QBitArray toBitArray (Object obj)
static boolean toBoolean (Object obj)
static QByteArray toByteArray (Object obj)
static char toChar (Object obj)
static QDate toDate (Object obj)
static QDateTime toDateTime (Object obj)
static double toDouble (Object obj, Boolean ok[])
static double toDouble (Object obj)
static int toInt (Object obj, Boolean ok[])
static int toInt (Object obj)
static QLine toLine (Object obj)
static QLineF toLineF (Object obj)
static List< Object > toList (Object obj)
static QLocale toLocale (Object obj)
static long toLong (Object obj, Boolean ok[])
static long toLong (Object obj)
static Map< String, Object > toMap (Object obj)
static QPoint toPoint (Object obj)
static QPointF toPointF (Object obj)
static QRect toRect (Object obj)
static QRectF toRectF (Object obj)
static QRegExp toRegExp (Object obj)
static QSize toSize (Object obj)
static QSizeF toSizeF (Object obj)
static String toString (Object obj)
static QTime toTime (Object obj)

Static Public Attributes

static final int BitArray = 13
static final int Bitmap = 73
static final int Boolean = 1
static final int Brush = 66
static final int ByteArray = 12
static final int Char = 7
static final int Color = 67
static final int Cursor = 74
static final int Date = 14
static final int DateTime = 16
static final int Double = 6
static final int Font = 64
static final int Icon = 69
static final int Image = 70
static final int Int = 2
static final int Invalid = 0
static final int KeySequence = 76
static final int Line = 23
static final int LineF = 24
static final int Locale = 18
static final int Long = 4
static final int Palette = 68
static final int Pen = 77
static final int Pixmap = 65
static final int Point = 25
static final int PointF = 26
static final int Polygon = 71
static final int Rect = 19
static final int RectF = 20
static final int RegExp = 27
static final int Region = 72
static final int Size = 21
static final int SizeF = 22
static final int SizePolicy = 75
static final int String = 10
static final int StringList = 11
static final int TextFormat = 79
static final int TextLength = 78
static final int Time = 15
static final int UserType = 127

Protected Member Functions

boolean __qt_signalInitialization (String name)
void disposed ()
final native void finalize ()

Package Types

enum  Types {
  Invalid = (0), Double = (6), String = (10), Boolean = (1),
  ByteArray = (12), BitArray = (13), Char = (7), Date = (14),
  DateTime = (16), Int = (2), Line = (23), LineF = (24),
  Locale = (18), Long = (4), Point = (25), PointF = (26),
  Rect = (19), RectF = (20), RegExp = (27), Size = (21),
  SizeF = (22), StringList = (11), Time = (15)
}

Static Package Functions

 [static initializer]

Static Package Attributes

static ThreadLocal
< QSignalEmitter
currentSender = new ThreadLocal<QSignalEmitter>()

Static Private Member Functions

static native boolean __qt_canConvert (Object obj, int type)
static native Object __qt_convert (int type, Object obj)
static native boolean __qt_toBoolean (Object obj)
static native char __qt_toChar (Object obj)
static native double __qt_toDouble (Object variant, Boolean ok[])
static native int __qt_toInt (Object obj, Boolean ok[])
static native long __qt_toLong (Object obj, Boolean ok[])
static native String __qt_toString (Object variant)
static void setOk (Boolean ok[], boolean isOk)

The documentation for this class was generated from the following file:

Generated by  Doxygen 1.6.0   Back to index