当前位置:网站首页>Summary of qvariant use in QT

Summary of qvariant use in QT

2022-06-23 06:43:00 litanyuan

background

QVariant It can store all kinds of data types , Include Qt All built-in types in 、C++ All basic data types in , And custom types .

Constructors

QVariant variant_bool(false);
QVariant variant_string(QString("hello"));
QVariant variant_double(3.14);
//... Custom types support direct construction 

Key member function

①.type: Get the currently saved data type

QVariant variant;
qDebug() << variant.type();

variant.setValue(3.14);
qDebug() << variant.type();

 Insert picture description here

②.value: Get the value saved to the specified type

QVariant variant(QString("hello"));
qDebug() << variant.value<QString>();

③.canConvert: Determine whether the stored type can be converted to the specified type

QVariant variant(3.14);
qDebug() << variant.type();
qDebug() << variant.canConvert<QString>();

 Insert picture description here

④.convert: Convert the storage to type to the specified type

QVariant variant(QString("hello"));
qDebug() << variant.type();

qDebug() << variant.convert(QVariant::Int);
qDebug() << variant;

 Insert picture description here

⑤.isValid: Determine whether a valid value is stored

QVariant variant;
qDebug() << variant.isValid();

variant = QString("hello");
qDebug() << variant.isValid();

 Insert picture description here

Basic data type reading

①. Save the data

QVariant variant_int(12);

QVariant variant_string = QString("hello");

QVariant variant_bool;
variant_bool.setValue(false);

②. data fetch

QVariant variant_int(12);
QVariant variant_string = QString("hello");
QVariant variant_bool;
variant_bool.setValue(false);

qDebug() << variant_int.toInt();
qDebug() << variant_string.toString();
qDebug() << variant_bool.value<bool>();

 Insert picture description here

Custom data type reading

①. Register as meta type

struct MyStruct
{
    
  int    id;
  QString name;
};
Q_DECLARE_METATYPE(MyStruct)
    
QVariant variant;
{
    
  MyStruct ms;
  ms.id = 1;
  ms.name = "hello";
  variant.setValue<MyStruct>(ms);
}

MyStruct ms = variant.value<MyStruct>();
qDebug() << ms.id << ms.name;

 Insert picture description here

②. Custom type conversion

struct MyStruct
{
    
  int    id;
  QString name;

  MyStruct() = default;
  MyStruct(const QVariant & variant)
  {
    
    *this = variant.value<MyStruct>();
  }

  operator QVariant() const
{
    
    return QVariant::fromValue(*this);
  }
  
};
Q_DECLARE_METATYPE(MyStruct)
       
QVariant variant;
{
    
  MyStruct ms;
  ms.id = 1;
  ms.name = "hello";
    
  variant = ms;
}

MyStruct ms = variant;
qDebug() << ms.id << ms.name;

 Insert picture description here

Pointer type access

MyStruct * ms = new MyStruct;
ms->id = 1;
ms->name = "hello";

QVariant variant = QVariant::fromValue(static_cast<void*>(ms));

MyStruct *  myStruct = static_cast<MyStruct*>(variant.value<void*>());
  
qDebug() << myStruct->id << myStruct->name;

 Insert picture description here

QVariantMap

①. summary

stay QVariantMap The header file has the following definitions :typedef QMap<QString,QVariant> QVariantMap, namely QVariantMap Is a type alias .

②. Code example

QVariantMap config;
config["id"] = 10;
config["name"] = QString("hello");

qDebug() << config["id"].toInt();
qDebug() << config["name"].toString();

 Insert picture description here

原网站

版权声明
本文为[litanyuan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230517246591.html