QVariant 数据的存与取,数据类型转换 (**)

目录

官方手册:fromValue,value(), qVariantFromValue

自定义结构体跟QVariant之间的转换

QVariant类及数据的存与取

===================================

官方手册:fromValue,value(), qVariantFromValue

[static] QVariant QVariant::fromValue(const T &value)

Returns a QVariant containing a copy of value. Behaves exactly like setValue() otherwise.
Example:

  MyCustomStruct s;
  return QVariant::fromValue(s);

Note: If you are working with custom types, you should use the Q_DECLARE_METATYPE() macro to register your custom type.
See also setValue() and value().

QVariant qVariantFromValue(const T &value)

This function is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.
Returns a variant containing a copy of the given value with template type T.
This function is equivalent to QVariant::fromValue(value).
Note: This function was provided as a workaround for MSVC 6 which did not support member template functions. It is advised to use the other form in new code.
For example, a QObject pointer can be stored in a variant with the following code:

  QObject *object = getObjectFromSomewhere();
  QVariant data = QVariant::fromValue(object);

See also QVariant::fromValue().

T QVariant::value() const

Returns the stored value converted to the template type T. Call canConvert() to find out whether a type can be converted. If the value cannot be converted, a default-constructed value will be returned.
If the type T is supported by QVariant, this function behaves exactly as toString(), toInt() etc.
Example:

  QVariant v;

  MyCustomStruct c;

                 
  if (v.canConvert<MyCustomStruct>())
      c = v.value<MyCustomStruct>();

                 

  v = 7;
  int i = v.value<int>();                        // same as v.toInt()
  QString s = v.value<QString>();                // same as v.toString(), s is now "7"
  MyCustomStruct c2 = v.value<MyCustomStruct>(); // conversion failed, c2 is empty

If the QVariant contains a pointer to a type derived from QObject then T may be any QObject type. If the pointer stored in the QVariant can be qobject_cast to T, then that result is returned. Otherwise nullptr is returned. Note that this only works for QObject subclasses which use the

Q_OBJECT macro.

If the QVariant contains a sequential container and T is QVariantList, the elements of the container will be converted into QVariants and returned as a QVariantList.

  QList<int> intList = {7, 11, 42};

  QVariant variant = QVariant::fromValue(intList);
  if (variant.canConvert<QVariantList>()) {
      QSequentialIterable iterable = variant.value<QSequentialIterable>();

  
      // Can use foreach:
      foreach (const QVariant &v, iterable) {
          qDebug() << v;
      }

 
      // Can use C++11 range-for:
      for (const QVariant &v : iterable) {
          qDebug() << v;
      }

 
      // Can use iterators:
      QSequentialIterable::const_iterator it = iterable.begin();
      const QSequentialIterable::const_iterator end = iterable.end();
      for ( ; it != end; ++it) {
          qDebug() << *it;
      }
  }

See also setValue(), fromValue(), canConvert(), and

Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE().

========================

自定义结构体跟QVariant之间的转换

【QT学习】自定义结构体跟QVariant之间的转换_qvariant转结构体_ipfpm的博客-CSDN博客

说明:自定义的结构体跟QVariant之间进行转换,主要是因为 Qt中变量的传递以及使用主要是以QVariant来进行的,通过转换可以实现变量传递,传递完成后再转成结构体,适用于qtwidget

(1)结构体跟QVariant之间的转换

    struct tytemp
    {
        unsigned int target;
        unsigned int current;
    };
    
    Q_DECLARE_METATYPE(tytemp)

 
    /*将结构体变成QVariant*/
    QVariant temp1;

         
    tytemp info;
    info.target  = 90;
    info.current = 100;

                 
    temp1 = QVariant::fromValue(info);


    /*将QVariant变成结构体*/
    tytemp tempinfo= temp1.value<tytemp>()

(2)QVariant跟list之间的转换

任何的qt的类型要转成QVariant都需要先看下qt的帮助文档,查看该qt类型是否是一个基本的类型。例如

    QVariantList --实际上就是--》QList<QVariant>
    QVariantMap --实际上就是--》QMap<QString, QVariant>

 将一个QList转成一个QVariant类型:

    Qlist<QVariant> list;

         
    list<<QVariant::fromValue(90)<<QVariant::fromValue(89)<<QVariant::fromValue(67);

         
    QVariant temp1;
    temp1 = QVariant::fromValue(list);

信号槽之间传递也可直接使用结构体:

https://mp.csdn.net/postedit/88699038

注:本来想将结构体变成QVariant之后,传递到qml中使用,发现不可行。

转自:

https://www.cnblogs.com/lvdongjie/p/4965801.html

QVariant类及数据的存与取

试想我们的 table单元格可以是 string,也可以是int,也可以是一个颜色值,那么这么多类型怎么返回呢?

于是,Qt提供了这个QVariant类型,你可以把这很多类型都存放进去,到需要使用的时候使用一系列的to函数取出来即可。

比如你把int包装成一个QVariant,使用的时候要用 QVariant::toInt()重新取出来。这里需要注意的是,QVariant类型的放入和取出,必须是相对应的,你放入一个int就必须按int取出,不能用toString(), Qt不会帮你自动转换

数据核心无非就是一个 union,和一个标记类型的type

传递的是整数 123,那么它union存储整数123,同时type标志Int;如果传递字符串,union存储字符串的指针,同时type标志QString

QVariant 可以保存很多Qt的数据类型,包括QBrush、QColor、QDateTime、QPixmap、QPoint和QString,并且还有C++基本类型,如 int、float等。

QVariant还能保存很多集合类型,如QMap<QString, QVariant>, QStringList和QList。item view classes,数据库模块和QSettings都大量使用了QVariant类,以方便我们读写数据。

示例:

    1.基本数据类型使用
    int
    QVariant var;
    var.setValue(123); //存
    int nvar ;
    nvar = var.value<int>();//取
    QString
    QVariant var2;
    var2.setValue(QString("world"));//存
    QString strValue;
    strValue = var2.value<QString>();//取


    2.QIcon等类使用
     QIcon icon("open.png");
     QVariant var3 = icon;        //存
     QIcon iconValue = var3.value<QIcon>();//取


    3.自定义结构体使用

    struct flyData
    {
        int id;
        QString name;
    };
    Q_DECLARE_METATYPE(flyData);//重点

    void xvalidator::slotSend()
    {
        flyData temp;
        temp.id = 1;
        temp.name = "test";
       

        QVariant data;
        data.setValue(temp);  //保存数据
        Receive( data);
    }

  void xvalidator::Receive(QVariant data)
   {
      flyData da = data.value<flyData>();    //获取数据
   }

————————————————
版权声明:本文为CSDN博主「yi-blog」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/liushiyi3083862599/article/details/92825446

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值