[Development] Serialising UI state in QML via QSettings and JSON: QByteArray vs QString

Mitch Curtis mitch.curtis at qt.io
Wed Oct 31 08:25:18 CET 2018



> -----Original Message-----
> From: J-P Nurmi <jpnurmi at gmail.com>
> Sent: Tuesday, 30 October 2018 3:52 PM
> To: Mitch Curtis <mitch.curtis at qt.io>
> Cc: development at qt-project.org
> Subject: Re: [Development] Serialising UI state in QML via QSettings and
> JSON: QByteArray vs QString
> 
> On Tue, Oct 30, 2018 at 3:04 PM Mitch Curtis <mitch.curtis at qt.io> wrote:
> > Their documentation doesn't claim that they operate on a specific type, so
> I'm not sure it's a matter of not doing what they claim to do.
> 
> They do promise to handle binary data, though. Representing binary data
> with null-terminated strings makes them useless. The sole purpose of base64
> is to encode binary data to text that can be represented as a string.
> 
> > Whether or not they could be extended to work with byte arrays is a
> question I don't have the answer to, though presumably it would involve
> modifying QV4::Value and who knows what else in the guts of QML.
> 
> The QML engine gained better QByteArray support recently (5.10?):
> http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#qbytearray-to-
> javascript-arraybuffer
> 
> > Unfortunately it wouldn't help the use case I'm trying to support though,
> because there is still the problem of Qt's JSON implementation not
> supporting byte arrays.
> 
> Pass the state as a base64 string. Where does it turn to a byte array if
> Qt.btoa() returns a string?

As of the previous patch set, saveState() returned a QByteArray, so the user starts off with a byte array. If I somehow manage to make Qt.atob() and Qt.btoa() accept and return a byte array (it wouldn't make sense to accept a byte array and return a string), this prevents writing such state to JSON due to its lack of support for byte arrays.

The only way to serialise SplitView's state to JSON if we changed these functions to use byte arrays would be to go through each value in the QVariantMap that is exposed to QML for it to set the UI state (QJsonObject can't be exposed to QML) and check if it's a byte array and then convert it to a Base64 string. That seems very hacky for users.

> > What do you think about the proposed solution of saveState() returning a
> Base64 QString?
> 
> -1 :)

Can you explain why? It's a negligible difference in binary vs text for the amount of data we're talking about.

> --
> J-P Nurmi


More information about the Development mailing list