当前位置: 首页>>代码示例>>C++>>正文


C++ QVariant::toJsonArray方法代码示例

本文整理汇总了C++中QVariant::toJsonArray方法的典型用法代码示例。如果您正苦于以下问题:C++ QVariant::toJsonArray方法的具体用法?C++ QVariant::toJsonArray怎么用?C++ QVariant::toJsonArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在QVariant的用法示例。


在下文中一共展示了QVariant::toJsonArray方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: setData

bool SceneModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    int row = index.row();
    if (row >= mNovel->scenes().count()){
        qWarning() << "set scene data: row" << row << "> novel scene count ("
                   << mNovel->scenes().count() << ")";
        return false;
    } else if (!index.isValid()){
        qWarning() << "set scene data: Invalid index";
        return false;
    }

    Scene *scene = mNovel->scenes()[row];

    QList<Character *> characters;

    if (role == Qt::DisplayRole || role == Qt::EditRole)
        scene->setHeadline(value.toString());
    else if (scene->plotline() && role == Qt::BackgroundRole){
        qWarning() << "set scene data: BackgroundRole is read-only";
        return false;
    }
    else if (role == HeadlineRole)
        scene->setHeadline(value.toString());
    else if (role == ActionRole)
        scene->setAction(value.toString());
    else if (role == PlotlineRole){
        Plotline *p = mNovel->plotline(QUuid(value.toString()));
        if (!p) qWarning() << "set scene data: Could not find plotline" << value.toInt();
        scene->setPlotline(p);
    }else if (role == CharactersRole){
        for (QJsonValue val : value.toJsonArray())
            characters << mNovel->character(QUuid(val.toString()));
        scene->setCharacters(characters);
    } else if (role == PointsOfViewRole) {
        for (QJsonValue val : value.toJsonArray())
            characters << mNovel->character(QUuid(val.toString()));
        scene->setPointsOfView(characters);
    }
    return true;
}
开发者ID:freckles-the-pirate,项目名称:plotline,代码行数:41,代码来源:sceneitemmodel.cpp

示例2: setData

void KNConfigure::setData(const QString &key, const QVariant &value)
{
    //Check whether the value is null.
    if(value.isNull())
    {
        //Remove the key from the configure data.
        m_dataObject.remove(key);
        //Complete.
        return;
    }
    //Because the QJsonObject can only insert QJsonValue, and the construct
    //function of QJsonValue only have the following types:
    //   bool, QString, array, double, object.
    //So we have to translate some complex type variant to object.
    switch(value.type())
    {
    //For the basic types(double, float, int, bool, QString), we will save them
    //directly.
    case QVariant::LongLong:
        m_dataObject.insert(key, value.toLongLong());
        break;
    case QVariant::Double:
        m_dataObject.insert(key, value.toDouble());
        break;
    case QVariant::String:
        m_dataObject.insert(key, value.toString());
        break;
    case QVariant::Int:
        m_dataObject.insert(key, value.toInt());
        break;
    case QVariant::Bool:
        m_dataObject.insert(key, value.toBool());
        break;
    //For advanced types(like Font), we have to translate them to a object.
    case QVariant::Font:
    {
        //Generate the font object.
        QFont font=value.value<QFont>();
        QJsonObject fontObject;
        fontObject.insert("Type", QString("Font"));
        fontObject.insert("Family", font.family());
        fontObject.insert("Size", font.pixelSize());
        fontObject.insert("Bold", font.bold());
        fontObject.insert("Italic", font.italic());
        fontObject.insert("Underline", font.underline());
        fontObject.insert("Strikeout", font.strikeOut());
        fontObject.insert("Kerning", font.kerning());
        //Insert the font object.
        m_dataObject.insert(key, fontObject);
        break;
    }
    case QVariant::KeySequence:
    {
        //Generate a key sequence object.
        QKeySequence keySequence=value.value<QKeySequence>();
        //A shortcut object.
        QJsonObject shortcutObject;
        shortcutObject.insert("Type", QString("Shortcut"));
        shortcutObject.insert("Key", keySequence.toString(
                                  QKeySequence::PortableText));
        //Insert the key sequence object.
        m_dataObject.insert(key, shortcutObject);
        break;
    }
    //For the JSON object, it is quite wired that QVariant doesn't enumerate the
    //JSON type here, but it will use the meta type json object here.
    case QMetaType::QJsonObject:
    {
        //Check with the value contains the custom type.
        QJsonObject customObject=value.toJsonObject();
        //Check the custom object type is matched.
        if("CustomObject"==customObject.value("Type").toString())
        {
            //Simply insert the json object to the data.
            m_dataObject.insert(key, customObject);
        }
        break;
    }
    //For the JSON array, directly save the original data.
    case QMetaType::QJsonArray:
    {
        //Simply insert the json array to the data.
        m_dataObject.insert(key, value.toJsonArray());
        break;
    }
    default:
        return;
    }
    //Emit the signal.
    emit valueChanged();
}
开发者ID:Kreogist,项目名称:Mu,代码行数:91,代码来源:knconfigure.cpp


注:本文中的QVariant::toJsonArray方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。