Qt 4.8
Classes | Typedefs | Functions
qvariant.h File Reference
#include <QtCore/qatomic.h>
#include <QtCore/qbytearray.h>
#include <QtCore/qlist.h>
#include <QtCore/qmetatype.h>
#include <QtCore/qmap.h>
#include <QtCore/qhash.h>
#include <QtCore/qstring.h>

Go to the source code of this file.

Classes

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

Typedefs

typedef QHash< QString, QVariantQVariantHash
 
typedef QList< QVariantQVariantList
 
typedef QMap< QString, QVariantQVariantMap
 

Functions

bool operator!= (const QVariant &v1, const QVariant &v2)
 
Q_CORE_EXPORT QDataStreamoperator<< (QDataStream &s, const QVariant &p)
 Writes a variant p to the stream s. More...
 
Q_CORE_EXPORT QDataStreamoperator<< (QDataStream &s, const QVariant::Type p)
 Writes a variant type p to the stream s. More...
 
Q_CORE_EXPORT QDebug operator<< (QDebug, const QVariant &)
 
Q_CORE_EXPORT QDebug operator<< (QDebug, const QVariant::Type)
 
bool operator== (const QVariant &v1, const QVariant &v2)
 
Q_CORE_EXPORT QDataStreamoperator>> (QDataStream &s, QVariant &p)
 Reads a variant p from the stream s. More...
 
Q_CORE_EXPORT QDataStreamoperator>> (QDataStream &s, QVariant::Type &p)
 Reads a variant type p in enum representation from the stream s. More...
 
 Q_DECLARE_TYPEINFO (QVariant, Q_MOVABLE_TYPE)
 
template<typename T >
qvariant_cast (const QVariant &)
 
template<>
QVariant qvariant_cast< QVariant > (const QVariant &v)
 
bool qvariant_cast_helper (const QVariant &v, QVariant::Type tp, void *ptr)
 
template<typename T >
QT_DEPRECATED bool qVariantCanConvert (const QVariant &variant)
 
template<typename T >
QVariant qVariantFromValue (const T &)
 
template<>
QVariant qVariantFromValue (const QVariant &t)
 
template<typename T >
void qVariantSetValue (QVariant &v, const T &t)
 
template<>
void qVariantSetValue< QVariant > (QVariant &v, const QVariant &t)
 
template<typename T >
QT_DEPRECATEDqVariantValue (const QVariant &variant)
 

Typedef Documentation

◆ QVariantHash

typedef QHash<QString, QVariant> QVariantHash
related

Definition at line 445 of file qvariant.h.

◆ QVariantList

typedef QList<QVariant> QVariantList
related

Definition at line 443 of file qvariant.h.

◆ QVariantMap

typedef QMap<QString, QVariant> QVariantMap
related

Definition at line 444 of file qvariant.h.

Function Documentation

◆ operator!=()

bool operator!= ( const QVariant v1,
const QVariant v2 
)
related

Referenced by QVariant::isDetached().

◆ operator<<() [1/4]

Q_CORE_EXPORT QDataStream& operator<< ( QDataStream s,
const QVariant p 
)

Writes a variant p to the stream s.

See also
Format of the QDataStream operators

Definition at line 2195 of file qvariant.cpp.

Referenced by QVariant::operator!=(), qVariantCanConvert(), and QVariant::setValue().

2196 {
2197  p.save(s);
2198  return s;
2199 }
void save(QDataStream &ds) const
Internal function for saving a variant to the stream s.
Definition: qvariant.cpp:2140

◆ operator<<() [2/4]

Q_CORE_EXPORT QDataStream& operator<< ( QDataStream s,
const QVariant::Type  p 
)

Writes a variant type p to the stream s.

Definition at line 2216 of file qvariant.cpp.

2217 {
2218  s << static_cast<quint32>(p);
2219 
2220  return s;
2221 }

◆ operator<<() [3/4]

Q_CORE_EXPORT QDebug operator<< ( QDebug  ,
const QVariant  
)

Definition at line 3108 of file qvariant.cpp.

3109 {
3110 #ifndef Q_BROKEN_DEBUG_STREAM
3111  dbg.nospace() << "QVariant(" << v.typeName() << ", ";
3112  QVariant::handler->debugStream(dbg, v);
3113  dbg.nospace() << ')';
3114  return dbg.space();
3115 #else
3116  qWarning("This compiler doesn't support streaming QVariant to QDebug");
3117  return dbg;
3118  Q_UNUSED(v);
3119 #endif
3120 }
static const Handler * handler
Definition: qvariant.h:419
Q_CORE_EXPORT void qWarning(const char *,...)
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ operator<<() [4/4]

Q_CORE_EXPORT QDebug operator<< ( QDebug  ,
const QVariant::Type   
)

Definition at line 3122 of file qvariant.cpp.

3123 {
3124 #ifndef Q_BROKEN_DEBUG_STREAM
3125  dbg.nospace() << "QVariant::" << QVariant::typeToName(p);
3126  return dbg.space();
3127 #else
3128  qWarning("This compiler doesn't support streaming QVariant::Type to QDebug");
3129  return dbg;
3130  Q_UNUSED(p);
3131 #endif
3132 }
static const char * typeToName(Type type)
Converts the enum representation of the storage type, typ, to its string representation.
Definition: qvariant.cpp:2008
Q_CORE_EXPORT void qWarning(const char *,...)
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ operator==()

bool operator== ( const QVariant v1,
const QVariant v2 
)
related

◆ operator>>() [1/2]

Q_CORE_EXPORT QDataStream& operator>> ( QDataStream s,
QVariant p 
)

Reads a variant p from the stream s.

Since
4.4
See also
Format of the QDataStream operators

Definition at line 2184 of file qvariant.cpp.

Referenced by QVariant::setValue().

2185 {
2186  p.load(s);
2187  return s;
2188 }
void load(QDataStream &ds)
Internal function for loading a variant from stream s.
Definition: qvariant.cpp:2093

◆ operator>>() [2/2]

Q_CORE_EXPORT QDataStream& operator>> ( QDataStream s,
QVariant::Type p 
)

Reads a variant type p in enum representation from the stream s.

Definition at line 2204 of file qvariant.cpp.

2205 {
2206  quint32 u;
2207  s >> u;
2208  p = (QVariant::Type)u;
2209 
2210  return s;
2211 }
quint16 u
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
unsigned int quint32
Definition: qglobal.h:938

◆ Q_DECLARE_TYPEINFO()

Q_DECLARE_TYPEINFO ( QVariant  ,
Q_MOVABLE_TYPE   
)

Referenced by qVariantCanConvert().

◆ qvariant_cast()

template<typename T >
T qvariant_cast ( const QVariant v)
related

Definition at line 571 of file qvariant.h.

Referenced by QStandardItem::accessibleDescription(), QStandardItem::accessibleText(), QDBusConnectionPrivate::activateCall(), QGraphicsScene::addItem(), QDBusMarshaller::append(), QDBusMarshaller::appendVariantInternal(), QTextHtmlParserNode::applyBackgroundImage(), QDeclarativeListAccessor::at(), QStandardItem::background(), QListWidgetItem::background(), QTableWidgetItem::background(), QTreeWidgetItem::background(), QListWidgetItem::backgroundColor(), QTableWidgetItem::backgroundColor(), QTreeWidgetItem::backgroundColor(), QAccessibleInterface::backgroundColor(), QOCIResultPrivate::bindValue(), QCss::ValueExtractor::borderValue(), QGraphicsItem::boundingRegionGranularity(), QScriptDebuggerCommand::breakpointData(), QTextFormat::brushProperty(), QCss::Declaration::brushValues(), QWindowsMimeImage::canConvertFromMime(), NestedListModel::checkRoles(), ModelNode::clear(), QTextFormat::colorProperty(), QCss::Declaration::colorValues(), QPaintBuffer::commandDescription(), QDeclarativeGestureAreaParser::compile(), QDeclarativeConnectionsParser::compile(), QDeclarativePropertyChangesParser::compile(), QDeclarativePropertyChangesParser::compileList(), QDeclarativeListModelParser::compileProperty(), QScript::ClassObjectDelegate::construct(), QMacPasteboardMimeTiff::convertFromMime(), QWindowsMimeImage::convertFromMime(), QDeclarativeListAccessor::count(), QDockAreaLayoutInfo::currentTabId(), QGraphicsItem::cursor(), NestedListModel::data(), QItemDelegate::decoration(), QTextFormat::doubleProperty(), QColorWell::dropEvent(), QColorShowLabel::dropEvent(), ModelNode::dump(), QMimeDataWrapper::encodedData(), QOCICols::execBatch(), QCss::ValueExtractor::extractBackground(), QFileSystemModel::fileIcon(), fillList(), fillList< float >(), QInputContext::font(), QListWidgetItem::font(), QStandardItem::font(), QTableWidgetItem::font(), QTreeWidgetItem::font(), QStandardItem::foreground(), QListWidgetItem::foreground(), QTableWidgetItem::foreground(), QTreeWidgetItem::foreground(), QAccessibleInterface::foregroundColor(), QWindowsMimeImage::formatsForMime(), NestedListModel::get(), QTextControl::getPaintContext(), QComboMenuDelegate::getStyleOption(), headerValue(), QStandardItem::icon(), QListWidgetItem::icon(), QTableWidgetItem::icon(), QTreeWidgetItem::icon(), QClipboard::image(), QStyledItemDelegate::initStyleOption(), QTextControlPrivate::inputMethodEvent(), QAccessibleAbstractSpinBox::invokeMethodEx(), QAccessibleAbstractSlider::invokeMethodEx(), QAccessibleTextEdit::invokeMethodEx(), QAccessibleLineEdit::invokeMethodEx(), QScriptDebuggerLocalsModelPrivate::isTopLevelNode(), QComboBoxPrivate::itemIcon(), QTextFormat::lengthProperty(), QHostInfo::lookupHost(), QGraphicsItemPrivate::maybeExtraItemCache(), QNetworkReplyImplPrivate::metaDataChanged(), ModelNode::model(), QAxServerBase::OnAmbientPropertyChange(), QDBusReply< QStringList >::operator=(), QItemDelegate::paint(), QHeaderView::paintEvent(), QHeaderView::paintSection(), parentWidget(), parseColorValue(), QTextFormat::penProperty(), QClipboard::pixmap(), QNetworkAccessHttpBackend::postRequest(), QPainterReplayer::process(), QPaintEngineExReplayer::process(), QLineControl::processInputMethodEvent(), QDBusAbstractInterfacePrivate::property(), QConnmanEngine::propertyChangedContext(), qdbus_cast(), qVariantValue(), QWinInputContext::reconvertString(), QItemDelegate::rect(), NestedListModel::remove(), QGraphicsScene::removeItem(), QInternalMimeData::renderDataHelper(), QScriptDebuggerResponse::resultAsBreakpointData(), QScriptDebuggerResponse::resultAsBreakpoints(), QScriptDebuggerResponse::resultAsContextInfo(), QScriptDebuggerResponse::resultAsScriptData(), QScriptDebuggerResponse::resultAsScripts(), QScriptDebuggerResponse::resultAsScriptValue(), QScriptDebuggerResponse::resultAsScriptValueList(), QScriptDebuggerResponse::resultAsScriptValuePropertyList(), QScriptDebuggerEvent::scriptValue(), QScriptDebuggerCommand::scriptValue(), scriptValueToMessage(), QHeaderView::sectionSizeFromContents(), NestedListModel::set(), QDeclarativeListModelParser::setCustomData(), QDeclarativeKeyNavigationAttached::setFocusNavigation(), QDeclarativeRepeater::setModel(), QDeclarativePathView::setModel(), QDeclarativeGridView::setModel(), QDeclarativeListView::setModel(), QDeclarativeVisualDataModel::setModel(), QItemDelegate::setOptions(), QGraphicsItem::setParentItem(), QVideoSurfaceFormat::setProperty(), NestedListModel::setProperty(), QDeclarativeVisualDataModel::setRootIndex(), QGraphicsItem::setTransform(), QUrlModel::setUrl(), QDeclarativePointFValueType::setValue(), QDeclarativePointValueType::setValue(), QDeclarativeSizeFValueType::setValue(), QDeclarativeSizeValueType::setValue(), QDeclarativeRectFValueType::setValue(), QDeclarativeRectValueType::setValue(), QDeclarativeVector2DValueType::setValue(), QDeclarativeVector3DValueType::setValue(), QDeclarativeVector4DValueType::setValue(), QDeclarativeQuaternionValueType::setValue(), QDeclarativeMatrix4x4ValueType::setValue(), QDeclarativeEasingValueType::setValue(), QDeclarativeFontValueType::setValue(), QStandardItem::sizeHint(), QListWidgetItem::sizeHint(), QTableWidgetItem::sizeHint(), QTreeWidgetItem::sizeHint(), QStyleSheetStyle::standardPixmap(), QStandardItem::statusTip(), QScriptDebuggerCommand::subordinateScriptValue(), QAccessibleInterface::supportedMethods(), QScriptDebuggerLocalsModel::sync(), QTextBlockFormat::tabPositions(), QStandardItem::text(), QListWidgetItem::textColor(), QTableWidgetItem::textColor(), QTreeWidgetItem::textColor(), QStandardItem::toolTip(), QWinInputContext::update(), QDeclarativeBinding::update(), ShaderEffectItem::updateEffectState(), ModelNode::updateListIndexes(), QDockAreaLayoutInfo::updateTabBar(), QVariant::value(), NestedListModel::valueForNode(), variantHash(), variantToQObject(), QDeclarativeWorkerScriptEnginePrivate::variantToScriptValue(), QSettingsPrivate::variantToString(), QAbstractSocket::waitForConnected(), QStandardItem::whatsThis(), QDeclarativePropertyPrivate::write(), QTextOdfWriter::writeInlineCharacter(), QDeclarativeVMEMetaObject::writeVarProperty(), QX11Data::xdndMimeDataForAtom(), xdndObtainData(), and QPaintBufferPrivate::~QPaintBufferPrivate().

572 {
573  const int vid = qMetaTypeId<T>(static_cast<T *>(0));
574  if (vid == v.userType())
575  return *reinterpret_cast<const T *>(v.constData());
576  if (vid < int(QMetaType::User)) {
577  T t;
578  if (qvariant_cast_helper(v, QVariant::Type(vid), &t))
579  return t;
580  }
581  return T();
582 }
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
bool qvariant_cast_helper(const QVariant &v, QVariant::Type tp, void *ptr)
Definition: qvariant.h:447
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
const void * constData() const
Definition: qvariant.cpp:3065

◆ qvariant_cast< QVariant >()

template<>
QVariant qvariant_cast< QVariant > ( const QVariant v)
inline

Definition at line 584 of file qvariant.h.

585 {
586  if (v.userType() == QMetaType::QVariant)
587  return *reinterpret_cast<const QVariant *>(v.constData());
588  return v;
589 }
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
const void * constData() const
Definition: qvariant.cpp:3065

◆ qvariant_cast_helper()

bool qvariant_cast_helper ( const QVariant v,
QVariant::Type  tp,
void *  ptr 
)
inline

Definition at line 447 of file qvariant.h.

Referenced by QVariant::operator!=(), and qvariant_cast().

448 { return QVariant::handler->convert(&v.d, tp, ptr, 0); }
static const Handler * handler
Definition: qvariant.h:419
const T * ptr(const T &t)
Private d
Definition: qvariant.h:417

◆ qVariantCanConvert()

template<typename T >
QT_DEPRECATED bool qVariantCanConvert ( const QVariant variant)
related

Definition at line 597 of file qvariant.h.

598 { return variant.template canConvert<T>(); }

◆ qVariantFromValue() [1/2]

template<typename T >
QVariant qVariantFromValue ( const T &  t)
related

◆ qVariantFromValue() [2/2]

template<>
QVariant qVariantFromValue ( const QVariant t)
inline

Definition at line 457 of file qvariant.h.

457 { return t; }

◆ qVariantSetValue()

template<typename T >
void qVariantSetValue ( QVariant v,
const T &  t 
)
related

Definition at line 460 of file qvariant.h.

Referenced by QVariant::setValue().

461 {
462  //if possible we reuse the current QVariant private
463  const uint type = qMetaTypeId<T>(reinterpret_cast<T *>(0));
464  QVariant::Private &d = v.data_ptr();
465  if (v.isDetached() && (type == d.type || (type <= uint(QVariant::Char) && d.type <= uint(QVariant::Char)))) {
466  d.type = type;
467  d.is_null = false;
468  T *old = reinterpret_cast<T*>(d.is_shared ? d.data.shared->ptr : &d.data.ptr);
470  old->~T();
471  new (old) T(t); //call the copy constructor
472  } else {
474  }
475 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
double d
Definition: qnumeric_p.h:62
int type
Definition: qmetatype.cpp:239
DataPtr & data_ptr()
Definition: qvariant.h:440
unsigned int uint
Definition: qglobal.h:996
bool isDetached() const
Definition: qvariant.h:537

◆ qVariantSetValue< QVariant >()

template<>
void qVariantSetValue< QVariant > ( QVariant v,
const QVariant t 
)
inline

Definition at line 478 of file qvariant.h.

479 {
480  v = t;
481 }

◆ qVariantValue()

template<typename T >
QT_DEPRECATED T qVariantValue ( const QVariant variant)
related

Definition at line 593 of file qvariant.h.

594 { return qvariant_cast<T>(variant); }
const char * variant
T qvariant_cast(const QVariant &value)
Returns the given value converted to the template type T.
Definition: qvariant.h:571