当前位置:网站首页>Use of qvariant
Use of qvariant
2022-07-25 22:41:00 【Things turn at will】
One 、 Introduce
QT According to the official document of :The QVariant class acts like a union for the most common Qt data types.QVariant It can store all kinds of data types ,QVariant Behavior similar to C/C++ Of union, But in Qt Middle ratio union A lot of powerful .
QVariant Built in support of all QMetaType::Type Classes declared in, such as :int,QString,QFont,QColor etc. , even to the extent that QList,QMap<QString, QVariant> Any complex type composed of .
Of course , If the supported type doesn't have what you want , No problem ,QVariant You can also support custom data types . By QVariant The stored data type needs a default constructor and a copy constructor . In order to achieve this function , First of all, you must use Q_DECLARE_METATYPE() macro . The header of this class is usually placed in the following file :
Q_DECLARE_METATYPE(MyClass)
Two 、 Use
Common variables
QVariant var1;
var1.setValue(12);
int data1 = var1.toInt();
qDebug() << data1;
QVariant var2 = 14;
int data2 = var2.toInt();
qDebug() << data2; 
Structure
#include <QtCore/QCoreApplication>
#include <QString>
#include <QtDebug>
#include <QVariant>
struct MyClass {
int id;
QString name;
};
Q_DECLARE_METATYPE(MyClass)
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyClass myClass;
myClass.id = 0;
myClass.name = QString("LiMing");
QVariant var3;
var3.setValue(myClass); // Set up QVariant Value
qDebug() << "var3:" << var3;
MyClass stu; // This part of the code is mainly about QVariant Class reconverted to MyClass class , other QVariant Class can also be transformed into other classes in this way
stu = var3.value<MyClass>();
qDebug() << "stu:" << stu.id << stu.name;
return a.exec();
} 
Save pointer
// preservation
QVariant var = QVariant::fromValue((void*)event);
// obtain
QPaintEvent* pointer = (QPaintEvent*)var.value<void*>();3、 ... and 、 Source code
open QVariant The definition of , We saw it use C++ Union
struct Private
{
union Data
{
char c;
uchar uc;
short s;
signed char sc;
ushort us;
int i;
uint u;
long l;
ulong ul;
bool b;
double d;
float f;
qreal real;
qlonglong ll;
qulonglong ull;
QObject *o;
void *ptr;
PrivateShared *shared;
} data;
uint type : 30;
uint is_shared : 1;
uint is_null : 1;
};
Types of support
enum 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
};
Constructors

Reference resources :
Qt And QVariant usage _ Snail lx The blog of -CSDN Blog _qvariant
QVariant Usage of _ Few blogs in ten years -CSDN Blog _qvariant
边栏推荐
- The new media operation strategy (taking xiaohongshu as an example) helps you quickly master the creative method of popular models
- [leetcode] 502.ipo (difficult)
- JVM内存区域
- If jimureport building block report is integrated according to the framework
- Domain oriented model programming
- Mitsubishi FX PLC free port RS command realizes Modbus Communication
- 【集训DAY15】好名字【hash】
- LabVIEW develops PCI-1680U dual port can card
- Examples and points for attention about the use of getchar and scanf
- (1) DDL, DML, DQL, DCL and common data types
猜你喜欢
随机推荐
Document flow definition, box model related knowledge
面试题 17.11. 单词距离 ●●
We media people must have four material websites, and don't worry about finding materials anymore
[database learning] redis parser & single thread & Model
MatrixCube揭秘102——300行实现的完整分布式存储系统MatrixKV
Severely crack down on illegal we media operators according to law: it is urgent to purify the we media industry
Build commercial projects based on ruoyi framework
分割金条的代价
Xiaobai programmer's fourth day
ORM common requirements
[training day15] simple calculation [tree array] [mathematics]
[training day15] paint road [minimum spanning tree]
Gan, why '𠮷 𠮷'.Length== 3 ??
Smart S7-200 PLC channel free mapping function block (do_map)
If it is modified according to the name of the framework module
Von Neumann architecture
Ffmpeg plays audio and video, time_ Base solves the problem of audio synchronization and SDL renders the picture
[MySQL rights] UDF rights (with Malaysia)
ECMA 262 12 Lexical Grammer
Solve several common problems









