|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.QVariant
public class QVariant
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 also supports the notion of null values, where you have
a defined type with no value set.
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 \c
toColor() function. Instead, you can use the QVariant::value() or
the qVariantValue() template function. For example:
The inverse conversion (e.g., from QColor to QVariant) is
automatic for all data types supported by QVariant, including
GUI-related types:
Because of Java's introspection, you should only use the QVariant
class when dealing with Qt Jambi classes that requires them.
QVariant can be extended to support other types than those
mentioned in the \l Type enum. See the \l QMetaType documentation
for details.
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
A Note on GUI Types
QVariant variant;
...
QColor color = variant.value
QColor color = palette().background().color();
QVariant variant = color;
Nested Class Summary
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9
Field Summary
static intBitArray
static intBitmap
static intBoolean
static intBrush
static intByteArray
static intChar
static intColor
static intCursor
static intDate
static intDateTime
static intDouble
static intFont
static intIcon
static intImage
static intInt
static intInvalid
static intKeySequence
static intLine
static intLineF
static intLocale
static intLong
static intPalette
static intPen
static intPixmap
static intPoint
static intPointF
static intPolygon
static intRect
static intRectF
static intRegExp
static intRegion
static intSize
static intSizeF
static intSizePolicy
static intString
static intStringList
static intTextFormat
static intTextLength
static intTime
static intUserType
Constructor Summary
QVariant()
Method Summary
static booleancanConvertToBitArray(java.lang.Object obj)
static booleancanConvertToBoolean(java.lang.Object obj)
static booleancanConvertToByteArray(java.lang.Object obj)
static booleancanConvertToChar(java.lang.Object obj)
static booleancanConvertToDate(java.lang.Object obj)
static booleancanConvertToDateTime(java.lang.Object obj)
static booleancanConvertToDouble(java.lang.Object obj)
static booleancanConvertToInt(java.lang.Object obj)
static booleancanConvertToLine(java.lang.Object obj)
static booleancanConvertToLineF(java.lang.Object obj)
static booleancanConvertToList(java.lang.Object obj)
static booleancanConvertToLocale(java.lang.Object obj)
static booleancanConvertToLong(java.lang.Object obj)
static booleancanConvertToMap(java.lang.Object obj)
static booleancanConvertToPoint(java.lang.Object obj)
static booleancanConvertToPointF(java.lang.Object obj)
static booleancanConvertToRect(java.lang.Object obj)
static booleancanConvertToRectF(java.lang.Object obj)
static booleancanConvertToRegExp(java.lang.Object obj)
static booleancanConvertToSize(java.lang.Object obj)
static booleancanConvertToSizeF(java.lang.Object obj)
static booleancanConvertToString(java.lang.Object obj)
static booleancanConvertToTime(java.lang.Object obj)
static QBitArraytoBitArray(java.lang.Object obj)
static booleantoBoolean(java.lang.Object obj)
static QByteArraytoByteArray(java.lang.Object obj)
static chartoChar(java.lang.Object obj)
static QDatetoDate(java.lang.Object obj)
static QDateTimetoDateTime(java.lang.Object obj)
static doubletoDouble(java.lang.Object obj)
static doubletoDouble(java.lang.Object obj,
java.lang.Boolean[] ok)
static inttoInt(java.lang.Object obj)
static inttoInt(java.lang.Object obj,
java.lang.Boolean[] ok)
static QLinetoLine(java.lang.Object obj)
static QLineFtoLineF(java.lang.Object obj)
static java.util.ListtoList(java.lang.Object obj)
static QLocaletoLocale(java.lang.Object obj)
static longtoLong(java.lang.Object obj)
static longtoLong(java.lang.Object obj,
java.lang.Boolean[] ok)
static java.util.MaptoMap(java.lang.Object obj)
static QPointtoPoint(java.lang.Object obj)
static QPointFtoPointF(java.lang.Object obj)
static QRecttoRect(java.lang.Object obj)
static QRectFtoRectF(java.lang.Object obj)
static QRegExptoRegExp(java.lang.Object obj)
static QSizetoSize(java.lang.Object obj)
static QSizeFtoSizeF(java.lang.Object obj)
static java.lang.StringtoString(java.lang.Object obj)
static QTimetoTime(java.lang.Object obj)
Methods inherited from class com.trolltech.qt.QtJambiObject
dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr
Methods inherited from class com.trolltech.qt.QSignalEmitter
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread
Methods inherited from class java.lang.Object
clone, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.trolltech.qt.QtJambiInterface
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership
Field Detail
Invalid
public static final int Invalid
Double
public static final int Double
String
public static final int String
Boolean
public static final int Boolean
ByteArray
public static final int ByteArray
BitArray
public static final int BitArray
Char
public static final int Char
Date
public static final int Date
DateTime
public static final int DateTime
Int
public static final int Int
Line
public static final int Line
LineF
public static final int LineF
Locale
public static final int Locale
Long
public static final int Long
Point
public static final int Point
PointF
public static final int PointF
Rect
public static final int Rect
RectF
public static final int RectF
RegExp
public static final int RegExp
Size
public static final int Size
SizeF
public static final int SizeF
StringList
public static final int StringList
Time
public static final int Time
Font
public static final int Font
Pixmap
public static final int Pixmap
Brush
public static final int Brush
Color
public static final int Color
Palette
public static final int Palette
Icon
public static final int Icon
Image
public static final int Image
Polygon
public static final int Polygon
Region
public static final int Region
Bitmap
public static final int Bitmap
Cursor
public static final int Cursor
SizePolicy
public static final int SizePolicy
KeySequence
public static final int KeySequence
Pen
public static final int Pen
TextLength
public static final int TextLength
TextFormat
public static final int TextFormat
UserType
public static final int UserType
Constructor Detail
QVariant
public QVariant()
Method Detail
canConvertToDouble
public static boolean canConvertToDouble(java.lang.Object obj)
toDouble
public static double toDouble(java.lang.Object obj)
toDouble
public static double toDouble(java.lang.Object obj,
java.lang.Boolean[] ok)
canConvertToString
public static boolean canConvertToString(java.lang.Object obj)
toString
public static java.lang.String toString(java.lang.Object obj)
canConvertToBoolean
public static boolean canConvertToBoolean(java.lang.Object obj)
toBoolean
public static boolean toBoolean(java.lang.Object obj)
canConvertToByteArray
public static boolean canConvertToByteArray(java.lang.Object obj)
toByteArray
public static QByteArray toByteArray(java.lang.Object obj)
canConvertToBitArray
public static boolean canConvertToBitArray(java.lang.Object obj)
toBitArray
public static QBitArray toBitArray(java.lang.Object obj)
canConvertToChar
public static boolean canConvertToChar(java.lang.Object obj)
toChar
public static char toChar(java.lang.Object obj)
canConvertToDate
public static boolean canConvertToDate(java.lang.Object obj)
toDate
public static QDate toDate(java.lang.Object obj)
canConvertToDateTime
public static boolean canConvertToDateTime(java.lang.Object obj)
toDateTime
public static QDateTime toDateTime(java.lang.Object obj)
canConvertToInt
public static boolean canConvertToInt(java.lang.Object obj)
toInt
public static int toInt(java.lang.Object obj)
toInt
public static int toInt(java.lang.Object obj,
java.lang.Boolean[] ok)
canConvertToLine
public static boolean canConvertToLine(java.lang.Object obj)
toLine
public static QLine toLine(java.lang.Object obj)
canConvertToLineF
public static boolean canConvertToLineF(java.lang.Object obj)
toLineF
public static QLineF toLineF(java.lang.Object obj)
canConvertToLocale
public static boolean canConvertToLocale(java.lang.Object obj)
toLocale
public static QLocale toLocale(java.lang.Object obj)
canConvertToPoint
public static boolean canConvertToPoint(java.lang.Object obj)
toPoint
public static QPoint toPoint(java.lang.Object obj)
canConvertToPointF
public static boolean canConvertToPointF(java.lang.Object obj)
toPointF
public static QPointF toPointF(java.lang.Object obj)
canConvertToRect
public static boolean canConvertToRect(java.lang.Object obj)
toRect
public static QRect toRect(java.lang.Object obj)
canConvertToRectF
public static boolean canConvertToRectF(java.lang.Object obj)
toRectF
public static QRectF toRectF(java.lang.Object obj)
canConvertToRegExp
public static boolean canConvertToRegExp(java.lang.Object obj)
toRegExp
public static QRegExp toRegExp(java.lang.Object obj)
canConvertToSize
public static boolean canConvertToSize(java.lang.Object obj)
toSize
public static QSize toSize(java.lang.Object obj)
canConvertToSizeF
public static boolean canConvertToSizeF(java.lang.Object obj)
toSizeF
public static QSizeF toSizeF(java.lang.Object obj)
canConvertToTime
public static boolean canConvertToTime(java.lang.Object obj)
toTime
public static QTime toTime(java.lang.Object obj)
canConvertToLong
public static boolean canConvertToLong(java.lang.Object obj)
toLong
public static long toLong(java.lang.Object obj)
toLong
public static long toLong(java.lang.Object obj,
java.lang.Boolean[] ok)
canConvertToList
public static boolean canConvertToList(java.lang.Object obj)
toList
public static java.util.List toList(java.lang.Object obj)
canConvertToMap
public static boolean canConvertToMap(java.lang.Object obj)
toMap
public static java.util.Map toMap(java.lang.Object obj)
Overview
Package
Class
Tree
Deprecated
Index
Help
PREV CLASS
NEXT CLASS
FRAMES
NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD
DETAIL: FIELD | CONSTR | METHOD