KernelPanic KernelPanic - 3 months ago 26
JSON Question

Subclassed QAbstractTableModel::setData with QJsonDocument as data source - QJsonValue::setValueAt is private

I am working on subclassed QAbstractTableModel with header:

#ifndef UEJSONPLACESTABLEMODEL_H
#define UEJSONPLACESTABLEMODEL_H

#include <QAbstractTableModel>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
#include <QVariant>
#include <QVariantMap>
#include <QVariantList>

class UeJsonPlacesTableModel : public QAbstractTableModel
{
private:
/**
* @brief m_ueJsonData
*/
QJsonDocument m_ueJsonData;

/*protected:*/
public:
/**
* @brief ueSetJsonData
* @param jsonData
*/
void ueSetJsonData(const QJsonDocument& jsonData);

public:
/**
* @brief UeJsonPlacesTableModel
* @param parent
*/
UeJsonPlacesTableModel(QObject* parent=Q_NULLPTR);

/**
* @brief ueJsonData
* @return pointer to object representing JSON document
*/
const QJsonDocument& ueJsonData()
{ return this->m_ueJsonData; }

/**
* @brief rowCount
* @param parent
* @return number of rows
*/
int rowCount(const QModelIndex& parent=QModelIndex()) const;

/**
* @brief columnCount
* @param parent
* @return number of columns
*/
int columnCount(const QModelIndex& parent=QModelIndex()) const;

/**
* @brief data
* @param index
* @param role
* @return data
*/
QVariant data(const QModelIndex& index,
int role=Qt::DisplayRole) const;

/**
* @brief headerData
* @param section
* @param orientation
* @param role
* @return header data
*/
QVariant headerData(int section,
Qt::Orientation orientation,
int role=Qt::DisplayRole) const;

/**
* @brief flags
* @param index
* @return flags for index's cell
*/
Qt::ItemFlags flags(const QModelIndex& index) const;

/**
* @brief setData
* @param index
* @param value
* @param role
* @return true if succesfull, otherwise false
*/
bool setData(const QModelIndex& index,
const QVariant& value,
int role=Qt::EditRole);
};

#endif // UEJSONPLACESTABLEMODEL_H


and its implementation:

#include "uejsonplacestablemodel.h"

UeJsonPlacesTableModel::UeJsonPlacesTableModel(QObject* parent)
: QAbstractTableModel(parent)
{
} // constructor

int UeJsonPlacesTableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)

return this->m_ueJsonData.isArray()?this->m_ueJsonData.array().size():this->m_ueJsonData.isObject()?this->m_ueJsonData.object().size():0;
} // rowCount

void UeJsonPlacesTableModel::ueSetJsonData(const QJsonDocument& jsonData)
{
this->beginResetModel();
this->m_ueJsonData=jsonData;
this->endResetModel();
} // ueSetJsonData

int UeJsonPlacesTableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)

return this->m_ueJsonData.isArray()?this->m_ueJsonData.array().at(0).toObject().keys().size():this->m_ueJsonData.isObject()?this->m_ueJsonData.object().keys().size():0;
} // columnCount

QVariant UeJsonPlacesTableModel::data(const QModelIndex& index,
int role) const
{
if(role!=Qt::DisplayRole||
index.row()<0||
index.row()>=this->m_ueJsonData.isArray()?this->m_ueJsonData.array().size():this->m_ueJsonData.isObject()?this->m_ueJsonData.object().size():0||
index.column()<0||
index.column()>=this->m_ueJsonData.isArray()?this->m_ueJsonData.array().at(0).toObject().keys().size():this->m_ueJsonData.isObject()?this->m_ueJsonData.object().keys().size():0)
{
return QVariant();
} // if

return this->m_ueJsonData.isArray()?this->m_ueJsonData.array().at(index.row()).toObject().value(this->m_ueJsonData.array().at(index.row()).toObject().keys().at(index.column())).toString():
this->m_ueJsonData.isObject()?this->m_ueJsonData.object().value(this->m_ueJsonData.array().at(index.row()).toObject().keys().at(index.column())).toString():
QVariant();
} // data

QVariant UeJsonPlacesTableModel::headerData(int section,
Qt::Orientation orientation,
int role) const
{
if(role!=Qt::DisplayRole||
section<0||
section>=this->m_ueJsonData.isArray()?this->m_ueJsonData.array().size():this->m_ueJsonData.isObject()?this->m_ueJsonData.object().size()>0?this->m_ueJsonData.object().size():0:0)
{
return QVariant();
} // if

switch(orientation)
{
case Qt::Horizontal:
{
return this->m_ueJsonData.isArray()?this->m_ueJsonData.array().size()>0?this->m_ueJsonData.array().at(0).toObject().keys().at(section):
this->m_ueJsonData.isObject()?this->m_ueJsonData.object().size()>0?this->m_ueJsonData.object().keys().at(section):
QVariant():
QVariant():
QVariant();
} // case

case Qt::Vertical:
{
return QAbstractTableModel::headerData(section,
orientation,
role);
} // case
} // switch

return QVariant();
} // headerData

Qt::ItemFlags UeJsonPlacesTableModel::flags(const QModelIndex& index) const
{
return index.column()==0?QAbstractTableModel::flags(index)^Qt::ItemIsEditable:QAbstractTableModel::flags(index);
} // flags

bool UeJsonPlacesTableModel::setData(const QModelIndex& index,
const QVariant& value,
int role)
{
if(role!=Qt::EditRole||
index.row()<0||
index.row()>=this->m_ueJsonData.isArray()?this->m_ueJsonData.array().size():this->m_ueJsonData.isObject()?this->m_ueJsonData.object().size():0||
index.column()<0||
index.column()>=this->m_ueJsonData.isArray()?this->m_ueJsonData.array().size():this->m_ueJsonData.isObject()?this->m_ueJsonData.object().size()>0?this->m_ueJsonData.object().size():0:0)
{
return false;
} // if

// if(this->m_ueJsonData.isArray())
// {
// QJsonObject jsonObject=this->m_ueJsonData.array().at(index.row()).toObject();

// jsonObject.setValueAt()
// } // if

return this->m_ueJsonData.isArray()?this->m_ueJsonData.array().at(index.row()).toObject().setValueAt(this->m_ueJsonData.object().keys().at(index.column()),
QJsonValue(value.toString()))?true:false:false;
} // setData


Now, in method
setData()
:

bool UeJsonPlacesTableModel::setData(const QModelIndex& index,
const QVariant& value,
int role)
{
if(role!=Qt::EditRole||
index.row()<0||
index.row()>=this->m_ueJsonData.isArray()?this->m_ueJsonData.array().size():this->m_ueJsonData.isObject()?this->m_ueJsonData.object().size():0||
index.column()<0||
index.column()>=this->m_ueJsonData.isArray()?this->m_ueJsonData.array().size():this->m_ueJsonData.isObject()?this->m_ueJsonData.object().size()>0?this->m_ueJsonData.object().size():0:0)
{
return false;
} // if

// if(this->m_ueJsonData.isArray())
// {
// QJsonObject jsonObject=this->m_ueJsonData.array().at(index.row()).toObject();

// jsonObject.setValueAt()
// } // if

return this->m_ueJsonData.isArray()?this->m_ueJsonData.array().at(index.row()).toObject().setValueAt(this->m_ueJsonData.object().keys().at(index.column()),
QJsonValue(value.toString()))?true:false:false;
} // setData


I am trying to change value of
QJsonObject
, but
QJsonObject::setValueAt()
is declared private, so I cannot use it. Are there any other options for changing some value in
QJsonDocument
?

Answer

An alternative, You could use: QVariant, QVariantMap and QVariantList, an example with QVariantMap:

QJsonDocument jsonDoc = QJsonDocument::fromJson(inputData.toUtf8());
QVariantMap map = jsonDoc.toVariant().toMap();
// Modify values
int value = map["example"].toInt();
map["example"] = value++;
qDebug() << "Current Value  " << map["example"].toInt();

At least, you could ubpate your jsondoc:

jsondoc = QJsonDocument::fromVariant(map);
// Notify that the data changed.
emit dataChanged(QModelIndex(), QModelIndex());
Comments