|
| typedef bool(* | Comparison )(const void *, const void *) |
| |
| typedef void *(* | Conversion )(const void *) |
| |
|
typedef void(* | IteratorDtorFunc )(void *) |
| |
|
typedef void *(* | IteratorCopyFunc )(void *) |
| |
|
typedef QVariant(* | IteratorValueFunc )(void *, int) |
| |
|
typedef void(* | IteratorIncFunc )(void *) |
| |
|
typedef void *(* | IteratorMoveFunc )(void *, int) |
| |
|
typedef int(* | IteratorDistanceFunc )(void *, void *) |
| |
|
typedef void *(* | IteratorStartEndFunc )(void *) |
| |
|
typedef bool(* | IteratorComparisonFunc )(void *, void *) |
| |
|
typedef int(* | IteratorCountFunc )(void *) |
| |
|
typedef void *(* | IteratorFindFunc )(void *, const QVariant &) |
| |
|
|
static bool | equal (const QVariant &left, const QVariant &right) |
| |
|
static bool | notEqual (const QVariant &left, const QVariant &right) |
| |
|
static bool | lessThan (const QVariant &left, const QVariant &right) |
| |
|
static bool | greaterThan (const QVariant &left, const QVariant &right) |
| |
|
static bool | lessEqualThan (const QVariant &left, const QVariant &right) |
| |
|
static bool | greaterEqualThan (const QVariant &left, const QVariant &right) |
| |
| static bool | canCompare (const QVariant &left, const QVariant &right) |
| |
| static bool | canCompare (const QVariant &left, int rightType) |
| |
| template<typename Right > |
| static bool | canCompare (const QVariant &left) |
| |
| template<typename Left , typename Right > |
| static bool | canCompare () |
| |
| static bool | canCompare (int leftType, int rightType) |
| |
| static QVariant | convert (const QVariant &variant, int type) |
| |
| template<typename T > |
| static QVariant | convert (const QVariant &variant) |
| |
| template<typename T > |
| static bool | canConvert (const QVariant &variant) |
| |
| static bool | canConvert (const QVariant &variant, int toType) |
| |
| static bool | canConvert (int fromType, int toType) |
| |
| template<typename T > |
| static T | toValue (const QVariant &variant) |
| |
| template<typename T > |
| static void | registerType () |
| |
| template<typename T , typename Right > |
| static void | registerType () |
| |
| static void | registerType (int type, int rightType, Comparison equal, Comparison lessThan) |
| |
| static void | registerType (int type, int rightType, Comparison equal, Comparison notEqual, Comparison less, Comparison greater, Comparison lessEqual, Comparison greaterEqual) |
| |
| template<typename From , typename To > |
| static void | registerConversion (To *(*func)(const From &)) |
| |
| template<typename From , typename To > |
| static void | registerConversion () |
| |
| static void | registerConversion (int from, int to, Conversion func) |
| |
| template<typename T > |
| static void | registerIterators (typename T::mapped_type *a=(typename T::mapped_type *) 0) |
| |
| template<typename T > |
| static void | registerIterators (typename T::value_type *a=(typename T::value_type *) 0) |
| |
| static Iterator | begin (QVariant &variant) |
| |
| static Iterator | end (QVariant &variant) |
| |
| static Iterator | find (QVariant &variant, QVariant ident) |
| |
| static int | itemCount (const QVariant &variant) |
| |
| static bool | isList (const QVariant &variant) |
| |
| static bool | isList (int typeId) |
| |
| static bool | isMap (const QVariant &variant) |
| |
| static bool | isMap (int typeId) |
| |
| static bool | isGeneric (const QVariant &variant) |
| |
| static bool | isGeneric (int typeId) |
| |
| static QVariantList | toList (QVariant variant) |
| |
| static QVariantMap | toMap (QVariant variant) |
| |
| template<typename... Items> |
| static QVariantList | buildList (const Items &...items) |
| |
| static void * | stealPointer (QVariant &variant) |
| |
The Variant class provides utilities for working with QVariants. Working with QVariants can be tedious if you have to work with them without knowing the structure of the data they hold. So, if you need to do convert a QVariant or want to compare them look no futher.
- Warning
- Do not use types with this class which use virtual methods as this can lead to crashes. If you want to use a type like this, register the pointer-type instead.
- Comparisons
- Variant provides multiple ways to compare two QVariants. First, if you haven't defined NURIA_NO_TEMPLATE_COMPARISON you're able to simply compare a QVariant to any class you registered to the Qt meta system: if (myQVariant < currentIndex) { // ...
Another possibility is to use the comparison methods as provided by Variant like equal or lessThan: if (Nuria::Variant::lessThan (myQVariant, currentIndex)) { // ...
To be able to compare QVariants you need to register your classes by using registerType.
- Note
- All registered classes must overload operator==() and operator<() correctly.
Comparisons are alyways 'smart'. If you try to compare two QVariants of non-comparable types, it will first try to convert the RHS QVariant to the type of the LHS QVariant. If this fails, it tries to convert the RHS to something the LHS can compare to. If this still fails, LHS is checked if it can be compared to QVariants. If yes these operators are used, if not false is returned. To check if two types are comparable use one of the many canCompare overloads.
- See also
- canCompare
- Warning
- A failed comparison always returns
false!
- Note
- The LHS won't be touched. All conversions are done to the RHS only.
- Conversions
- Another feature that QVariant is lacking is the ability to convert a QVariant to or from a user type. This means you can register conversion functions which take type A and return a pointer to an instance of type B.
- See also
- convert canConvert registerConversion
It is also possible to check beforehand if a certain conversion is possible at all using one of the various canConvert() overloads:
- Note
- Custom conversions take precedence over the default Qt ones!
- Iterating over a variant
- A variant can hold multiple items if it contains a QStringList or a QVariantList. Sometimes it would be neat if you could iterate over this list without knowing its exact type, right? This is where VARIANT_FOREACH comes in. This little macro works like Q_FOREACH, with the exception it always expects a QVariant instead of a container. VARIANT_FOREACH iterates over any iteratorable type.
- See also
- registerIterators
Example:
QVariant a ("Hello"), b (QStringList() << "One" << "Two" << "Three");
VARIANT_FOREACH(const QVariant &cur, a) qDebug() << cur;
VARIANT_FOREACH(const QVariant &cur, b) qDebug() << cur;
If you need more control though, there is Variant::Iterator for you. It works like all Qt STL-style iterators.
- See also
- begin end Iterator