QT QVariant

来源:互联网 发布:深圳 人工智能 政策 编辑:程序博客网 时间:2024/05/22 03:08

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());

原创粉丝点击