QStandardItemModel类中的函数
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE;
中有一个QVariant类型变量。
QVariant类类似于C++联合(union)数据类型,它不仅能够保存很多Qt类型的值,包括QColor,QBrush,QFont,QPen,QString和QSize等,也能够存放Qt的容器类型的值。Qt的很多功能都是建立在QVariant基础上的,如Qt的对象属性及数据库功能等。
DEMO
#include "widget.h"
#include <QDebug>
#include <QVariant>
#include <QColor>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QVariant v(709);
qDebug()<<v.toInt();
QVariant w("how are you!");
qDebug()<<w.toString();
//声明一个QMap变量map,使用字符串作为键,QVariant变量作为值
QMap<QString,QVariant>map;
map["int"]=709;
map["double"]=709.709;
map["string"]="how are you!"; //输入字符串
map["color"]=QColor(255,0,0);
//调用相应的转换函数并输出
qDebug()<<map["int"]<<map["int"].toInt();
qDebug()<<map["double"]<<map["double"].toDouble();
qDebug()<<map["string"]<<map["string"].toString();
qDebug()<<map["color"]<<map["color"].value<QColor>();
QStringList sl; //创建一个字符串列表
sl<<"A"<<"B"<<"C"<<"D";
QVariant slv(sl); //将该列表保存在一个QVariant变量中
//QVariant::type()函数返回存储在QVariant变量中的值的数据类型。
if(slv.type() == QVariant::StringList)
{
QStringList list=slv.toStringList();
for(int i=0;i<list.size();++i)
{
qDebug()<<list.at(i); //输出列表内容
}
}
}
Widget::~Widget()
{
}
运行结果
Qt的常用QVariant::type枚举类型变量
Invalid = QMetaType::UnknownType,
Bool = QMetaType::Bool,
Int = QMetaType::Int,
UInt = QMetaType::UInt,
LongLong = QMetaType::LongLong,
ULongLong = QMetaType::ULongLong,
Double = QMetaType::Double,
Char = QMetaType::QChar,
Map = QMetaType::QVariantMap,
List = QMetaType::QVariantList,
String = QMetaType::QString,
StringList = QMetaType::QStringList,
ByteArray = QMetaType::QByteArray,
BitArray = QMetaType::QBitArray,
Date = QMetaType::QDate,
Time = QMetaType::QTime,
DateTime = QMetaType::QDateTime,
Url = QMetaType::QUrl,
Locale = QMetaType::QLocale,
Rect = QMetaType::QRect,
RectF = QMetaType::QRectF,
Size = QMetaType::QSize,
SizeF = QMetaType::QSizeF,
Line = QMetaType::QLine,
LineF = QMetaType::QLineF,
Point = QMetaType::QPoint,
PointF = QMetaType::QPointF,
RegExp = QMetaType::QRegExp,
RegularExpression = QMetaType::QRegularExpression,
Hash = QMetaType::QVariantHash,
EasingCurve = QMetaType::QEasingCurve,
Uuid = QMetaType::QUuid,
ModelIndex = QMetaType::QModelIndex,
PersistentModelIndex = QMetaType::QPersistentModelIndex,
LastCoreType = QMetaType::LastCoreType,
Font = QMetaType::QFont,
Pixmap = QMetaType::QPixmap,
Brush = QMetaType::QBrush,
Color = QMetaType::QColor,
Palette = QMetaType::QPalette,
Image = QMetaType::QImage,
Polygon = QMetaType::QPolygon,
Region = QMetaType::QRegion,
Bitmap = QMetaType::QBitmap,
Cursor = QMetaType::QCursor,
KeySequence = QMetaType::QKeySequence,
Pen = QMetaType::QPen,
TextLength = QMetaType::QTextLength,
TextFormat = QMetaType::QTextFormat,
Matrix = QMetaType::QMatrix,
Transform = QMetaType::QTransform,
Matrix4x4 = QMetaType::QMatrix4x4,
Vector2D = QMetaType::QVector2D,
Vector3D = QMetaType::QVector3D,
Vector4D = QMetaType::QVector4D,
Quaternion = QMetaType::QQuaternion,
PolygonF = QMetaType::QPolygonF,
Icon = QMetaType::QIcon,
LastGuiType = QMetaType::LastGuiType,
SizePolicy = QMetaType::QSizePolicy,
UserType = QMetaType::User,
LastType = 0xffffffff // need this so that gcc >= 3.4 allocates 32 bits for Type