Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

QVariant Class Reference

Inheritance diagram for QVariant:

QDBusVariant

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

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

Definition at line 98 of file qvariant.h.


Public Types

typedef Private DataPtr
typedef bool(* f_canConvert )(const QVariant::Private *d, Type t)
typedef void(* f_clear )(Private *)
typedef bool(* f_compare )(const Private *, const Private *)
typedef void(* f_construct )(Private *, const void *)
typedef bool(* f_convert )(const QVariant::Private *d, Type t, void *, bool *)
typedef void(* f_debugStream )(QDebug, const QVariant &)
typedef void(* f_load )(Private *, QDataStream &)
typedef bool(* f_null )(const Private *)
typedef void(* f_save )(const Private *, QDataStream &)
enum  Type {
  Invalid = 0, Bool = 1, Int = 2, UInt = 3,
  LongLong = 4, ULongLong = 5, Double = 6, Char = 7,
  Map = 8, List = 9, String = 10, StringList = 11,
  ByteArray = 12, BitArray = 13, Date = 14, Time = 15,
  DateTime = 16, Url = 17, Locale = 18, Rect = 19,
  RectF = 20, Size = 21, SizeF = 22, Line = 23,
  LineF = 24, Point = 25, PointF = 26, RegExp = 27,
  LastCoreType = RegExp, Font = 64, Pixmap = 65, Brush = 66,
  Color = 67, Palette = 68, Icon = 69, Image = 70,
  Polygon = 71, Region = 72, Bitmap = 73, Cursor = 74,
  SizePolicy = 75, KeySequence = 76, Pen = 77, TextLength = 78,
  TextFormat = 79, Matrix = 80, Transform = 81, LastGuiType = Transform,
  UserType = 127, LastType = 0xffffffff
}

Public Member Functions

template<typename T>
bool canConvert () const
bool canConvert (Type t) const
void clear ()
const void * constData () const
bool convert (Type t)
const void * data () const
void * data ()
DataPtrdata_ptr ()
void detach ()
bool isDetached () const
bool isNull () const
bool isValid () const
void load (QDataStream &ds)
bool operator!= (const QVariant &v) const
QVariantoperator= (const QVariant &other)
bool operator== (const QVariant &v) const
 QVariant (Qt::GlobalColor color)
 QVariant (const QRegExp &regExp)
 QVariant (const QLocale &locale)
 QVariant (const QUrl &url)
 QVariant (const QRectF &rect)
 QVariant (const QRect &rect)
 QVariant (const QLineF &line)
 QVariant (const QLine &line)
 QVariant (const QPointF &pt)
 QVariant (const QPoint &pt)
 QVariant (const QSizeF &size)
 QVariant (const QSize &size)
 QVariant (const QMap< QString, QVariant > &map)
 QVariant (const QList< QVariant > &list)
 QVariant (const QDateTime &datetime)
 QVariant (const QTime &time)
 QVariant (const QDate &date)
 QVariant (const QChar &qchar)
 QVariant (const QStringList &stringlist)
 QVariant (const QLatin1String &string)
 QVariant (const QString &string)
 QVariant (const QBitArray &bitarray)
 QVariant (const QByteArray &bytearray)
QT_ASCII_CAST_WARN_CONSTRUCTOR QVariant (const char *str)
 QVariant (double d)
 QVariant (bool b)
 QVariant (qulonglong ull)
 QVariant (qlonglong ll)
 QVariant (uint ui)
 QVariant (int i)
 QVariant (QDataStream &s)
 QVariant (const QVariant &other)
 QVariant (int typeOrUserType, const void *copy)
 QVariant (Type type)
 QVariant ()
void save (QDataStream &ds) const
template<typename T>
void setValue (const T &value)
QBitArray toBitArray () const
bool toBool () const
QByteArray toByteArray () const
QChar toChar () const
QDate toDate () const
QDateTime toDateTime () const
double toDouble (bool *ok=0) const
int toInt (bool *ok=0) const
QLine toLine () const
QLineF toLineF () const
QList< QVarianttoList () const
QLocale toLocale () const
qlonglong toLongLong (bool *ok=0) const
QMap< QString, QVarianttoMap () const
QPoint toPoint () const
QPointF toPointF () const
QRect toRect () const
QRectF toRectF () const
QRegExp toRegExp () const
QSize toSize () const
QSizeF toSizeF () const
QString toString () const
QStringList toStringList () const
QTime toTime () const
uint toUInt (bool *ok=0) const
qulonglong toULongLong (bool *ok=0) const
QUrl toUrl () const
Type type () const
const char * typeName () const
int userType () const
template<typename T>
value () const
 ~QVariant ()

Static Public Member Functions

template<typename T>
static QVariant fromValue (const T &value)
static Type nameToType (const char *name)
static const char * typeToName (Type type)

Protected Member Functions

bool cmp (const QVariant &other) const
void create (int type, const void *copy)

Protected Attributes

Private d

Static Protected Attributes

static const Handler * handler = &qt_kernel_variant_handler

Private Member Functions

 QVariant (bool, int)

Friends

Q_CORE_EXPORT QDebug operator<< (QDebug, const QVariant &)
bool operator== (const QVariant &, const QVariantComparisonHelper &)
int qRegisterGuiVariant ()
int qUnregisterGuiVariant ()
bool qvariant_cast_helper (const QVariant &, QVariant::Type, void *)

Related Functions

(Note that these are not member functions.)

bool operator!= (const QVariant &v1, const QVariant &v2)
bool operator== (const QVariant &v1, const QVariant &v2)
qvariant_cast (const QVariant &value)
bool qVariantCanConvert (const QVariant &value)
QVariant qVariantFromValue (const T &value)
 QVariantList
 QVariantMap
void qVariantSetValue (QVariant &variant, const T &value)
qVariantValue (const QVariant &value)

Classes

struct  Handler
struct  Private
struct  PrivateShared

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

Generated by  Doxygen 1.6.0   Back to index