1. 程式人生 > >Qt——QVariant隱式型別轉換實現型別系統(Type System)

Qt——QVariant隱式型別轉換實現型別系統(Type System)

    QVariant v(709);
    qDebug() << v.toInt();

    QVariant w("How are you! ");
    qDebug() << w.toString();

    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 s1;
    s1 << "A" << "B" << "C" << "D";
    QVariant s1v(s1);
    if (s1v.type() == QVariant::StringList)
    {
        QStringList list = s1v.toStringList();
        for (int i = 0; i < list.size(); ++i)
            qDebug() << list.at(i);
    }

輸出結果:

709

"How are you! "

QVariant(int, 709) 709

QVariant(double, 709.709) 709.709

QVariant(QString, "How Are You! ") "How Are You! "

QVariant(QColor, QColor(ARGB 1, 1, 0, 0) ) QColor(ARGB 1, 1, 0, 0)

"A"

"B"

"C"

"D"