[Development] Best-practice data type to pass JSON data between C++ and QML

Simon Hausmann simon.hausmann at digia.com
Thu Aug 8 17:12:46 CEST 2013


On Thursday 8. August 2013 17.04.38 Milian Wolff wrote:
> On Thursday 08 August 2013 10:44:36 Jedrzej Nowacki wrote:
> > Have you tried QJSValue? It is a wrapper around js object so in theory no
> > conversion is needed
> 
> I don't see a way to construct a QJSValue which holds a JSON object.

I think

QJSValue value = engine->toScriptValue(jsonObject);

should work.

But regardless, conversions are always involved and they suck :(. Best is to 
either do everything on the JavaScript side (often underestimated!) or C++ 
side and minimize the communcation as much as possible.


Simon

> > On Wednesday 7. August 2013 17.17.19 Milian Wolff wrote:
> > > Hey there!
> > > 
> > > What data type is the most efficient to pass JSON data between C++ and
> > > QML?
> > > Is there one which does not incur any conversions at all? I tried with
> > > QVariantMap and QJsonObject, both work more or less equally well I
> > > think.
> > > Are both the best you can get?
> > > 
> > > I'm interested in both ways btw., i.e.:
> > > 
> > > test.qml
> > > Item {
> > > 
> > >   function foo() {
> > >   
> > >     for(var i in myObj.data) {
> > >     
> > >       console.log(i, myObj.data[i]);
> > >     
> > >     }
> > >     myObj.data = {foo: "bar", asdf: 42.0};
> > > 
> > > }
> > > 
> > > 
> > > MyObj.h
> > > 
> > > typedef JSONData ?;
> > > 
> > > class MyObj : public QObject {
> > > 
> > >   Q_OBJECT
> > >   Q_PROPERTY(data READ data WRITE setData NOTIFY dataChanged)
> > > 
> > > public:
> > >   JSONData data() const;
> > >   void setData(const JSONData& data) const;
> > > 
> > > signals:
> > >   void dataChanged(JSONData);
> > > 
> > > };
> > > 
> > > Thanks



More information about the Development mailing list