⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qvariant.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
            return false;        break;    case QVariant::Point:        if (d->type == QVariant::PointF)            *static_cast<QPoint *>(result) = (v_cast<QPointF>(d))->toPoint();        else            return false;        break;    case QMetaType::Char:    {        *static_cast<qint8 *>(result) = qint8(qConvertToNumber(d, ok));        return *ok;    }#endif    default:        return false;    }    return true;}#if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)static void streamDebug(QDebug dbg, const QVariant &v){    switch (v.type()) {    case QVariant::Int:        dbg.nospace() << v.toInt();        break;    case QVariant::UInt:        dbg.nospace() << v.toUInt();        break;    case QVariant::LongLong:        dbg.nospace() << v.toLongLong();        break;    case QVariant::ULongLong:        dbg.nospace() << v.toULongLong();        break;    case QVariant::Double:        dbg.nospace() << v.toDouble();        break;    case QVariant::Bool:        dbg.nospace() << v.toBool();        break;    case QVariant::String:        dbg.nospace() << v.toString();        break;    case QVariant::Char:        dbg.nospace() << v.toChar();        break;    case QVariant::StringList:        dbg.nospace() << v.toStringList();        break;    case QVariant::Map:        dbg.nospace() << v.toMap();        break;    case QVariant::List:        dbg.nospace() << v.toList();        break;    case QVariant::Date:        dbg.nospace() << v.toDate();        break;    case QVariant::Time:        dbg.nospace() << v.toTime();        break;    case QVariant::DateTime:        dbg.nospace() << v.toDateTime();        break;    case QVariant::ByteArray:        dbg.nospace() << v.toByteArray();        break;    case QVariant::Url:        dbg.nospace() << v.toUrl();        break;#ifndef QT_NO_GEOM_VARIANT    case QVariant::Point:        dbg.nospace() << v.toPoint();        break;    case QVariant::PointF:        dbg.nospace() << v.toPointF();        break;    case QVariant::Rect:        dbg.nospace() << v.toRect();        break;    case QVariant::Size:        dbg.nospace() << v.toSize();        break;    case QVariant::SizeF:        dbg.nospace() << v.toSizeF();        break;    case QVariant::Line:        dbg.nospace() << v.toLine();        break;    case QVariant::LineF:        dbg.nospace() << v.toLineF();        break;    case QVariant::RectF:        dbg.nospace() << v.toRectF();        break;#endif    case QVariant::BitArray:        //dbg.nospace() << v.toBitArray();        break;    default:        break;    }}#endifconst QVariant::Handler qt_kernel_variant_handler = {    construct,    clear,    isNull,#ifndef QT_NO_DATASTREAM    0,    0,#endif    compare,    convert,    0,#if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)    streamDebug#else    0#endif};Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler(){    return &qt_kernel_variant_handler;}const QVariant::Handler *QVariant::handler = &qt_kernel_variant_handler;/*!    \class QVariant    \brief The QVariant class acts like a union for the most common Qt data types.    \ingroup objectmodel    \ingroup misc    \ingroup shared    \mainclass    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:    \code        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());    \endcode    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.    \code        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    \endcode    QVariant can be extended to support other types than those    mentioned in the \l Type enum. See the \l QMetaType documentation    for details.    \section1 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 \c    toColor() function. Instead, you can use the QVariant::value() or    the qVariantValue() template function. For example:    \code        QVariant variant;        ...        QColor color = variant.value<QColor>();    \endcode    The inverse conversion (e.g., from QColor to QVariant) is    automatic for all data types supported by QVariant, including    GUI-related types:    \code        QColor color = palette().background().color();        QVariant variant = color;    \endcode    \section1 Using canConvert() and convert() Consecutively    When using canConvert() and convert() consecutively, it is possible for    canConvert() to return true, but convert() to return false. This    is typically because canConvert() only reports the general ability of    QVariant to convert between types given suitable data; it is still    possible to supply data which cannot actually be converted.    For example, canConvert() would return true when called on a variant    containing a string because, in principle, QVariant is able to convert    strings of numbers to integers.    However, if the string contains non-numeric characters, it cannot be    converted to an integer, and any attempt to convert it will fail.    Hence, it is important to have both functions return true for a    successful conversion.    \sa QMetaType*//*!    \enum QVariant::Type    This enum type defines the types of variable that a QVariant can    contain.    \value Invalid  no type    \value BitArray  a QBitArray    \value Bitmap  a QBitmap    \value Bool  a bool    \value Brush  a QBrush    \value ByteArray  a QByteArray    \value Char  a QChar    \value Color  a QColor    \value Cursor  a QCursor    \value Date  a QDate    \value DateTime  a QDateTime    \value Double  a double    \value Font  a QFont    \value Icon  a QIcon    \value Image  a QImage    \value Int  an int    \value KeySequence  a QKeySequence    \value Line  a QLine    \value LineF  a QLineF    \value List  a QVariantList    \value Locale  a QLocale    \value LongLong a \l qlonglong    \value Map  a QVariantMap    \value Matrix  a QMatrix    \value Transform  a QTransform    \value Palette  a QPalette    \value Pen  a QPen    \value Pixmap  a QPixmap    \value Point  a QPoint    \value PointArray  a QPointArray    \value PointF  a QPointF    \value Polygon a QPolygon    \value Rect  a QRect    \value RectF  a QRectF    \value RegExp  a QRegExp    \value Region  a QRegion    \value Size  a QSize    \value SizeF  a QSizeF    \value SizePolicy  a QSizePolicy    \value String  a QString    \value StringList  a QStringList    \value TextFormat  a QTextFormat    \value TextLength  a QTextLength    \value Time  a QTime    \value UInt  a \l uint    \value ULongLong a \l qulonglong    \value Url  a QUrl    \value UserType Base value for user-defined types.    \omitvalue CString    \omitvalue ColorGroup    \omitvalue IconSet    \omitvalue LastGuiType    \omitvalue LastCoreType    \omitvalue LastType*//*!    \fn QVariant::QVariant()    Constructs an invalid variant.*//*!    \fn QVariant::QVariant(int typeOrUserType, const void *copy)    Constructs variant of type \a typeOrUserType, and initializes with    \a copy if \a copy is not 0.    Note that you have to pass the address of the variable you want stored.    Usually, you never have to use this constructor, use qVariantFromValue()    instead to construct variants from the pointer types represented by    \c QMetaType::VoidStar, \c QMetaType::QObjectStar and    \c QMetaType::QWidgetStar.    \sa qVariantFromValue(), Type*//*!    \fn QVariant::QVariant(Type type)    Constructs a null variant of type \a type.*//*!    \fn QVariant::create(int type, const void *copy)    \internal    Constructs a variant private of type \a type, and initializes with \a copy if    \a copy is not 0.*/void QVariant::create(int type, const void *copy){    d.type = type;    handler->construct(&d, copy);}/*!    \fn QVariant::~QVariant()    Destroys the QVariant and the contained object.    Note that subclasses that reimplement clear() should reimplement    the destructor to call clear(). This destructor calls clear(), but    because it is the destructor, QVariant::clear() is called rather    than a subclass's clear().*/QVariant::~QVariant(){    if (d.type > Char && (!d.is_shared || !d.data.shared->ref.deref()))        handler->clear(&d);}/*!  \fn QVariant::QVariant(const QVariant &p)    Constructs a copy of the variant, \a p, passed as the argument to    this constructor.*/QVariant::QVariant(const QVariant &p)    : d(p.d){    if (d.is_shared) {        d.data.shared->ref.ref();    } else if (p.d.type > Char) {        handler->construct(&d, p.constData());        d.is_null = p.d.is_null;    }}#ifndef QT_NO_DATASTREAM/*!    Reads the variant from the data stream, \a s.*/QVariant::QVariant(QDataStream &s){    d.is_null = true;    s >> *this;}#endif //QT_NO_DATASTREAM/*!  \fn QVariant::QVariant(const QString &val)    Constructs a new variant with a string value, \a val.*//*!  \fn QVariant::QVariant(const QLatin1String &val)    Constructs a new variant with a string value, \a val.*//*!  \fn QVariant::QVariant(const char *val)    Constructs a new variant with a string value of \a val.    The variant creates a deep copy of \a val, using the encoding    set by QTextCodec::setCodecForCStrings().    You can disable this operator by defining \c    QT_NO_CAST_FROM_ASCII when you compile your applications.    \sa QTextCodec::setCodecForCStrings()*/#ifndef QT_NO_CAST_FROM_ASCIIQVariant::QVariant(const char *val){    QString s = QString::fromAscii(val);    create(String, &s);}#endif/*!  \fn QVariant::QVariant(const QStringList &val)    Constructs a new variant with a string list value, \a val.*//*!  \fn QVariant::QVariant(const QMap<QString, QVariant> &val)    Constructs a new variant with a map of QVariants, \a val.*//*!  \fn QVariant::QVariant(const QDate &val)    Constructs a new variant with a date value, \a val.*//*!  \fn QVariant::QVariant(const QTime &val)    Constructs a new variant with a time value, \a val.*//*!  \fn QVariant::QVariant(const QDateTime &val)    Constructs a new variant with a date/time value, \a val.*//*!  \fn QVariant::QVariant(const QByteArray &val)    Constructs a new variant with a bytearray value, \a val.*//*!

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -