Important: Please read the Qt Code of Conduct -

Link QByteArray to another container with different datatype

  • I have a QByteArray object filled with the contents of a file and I do some operations on it like replacing some bytes with others at some place in that array.

    But I need to rearrange this data to make it useful to a list of quint16 in a little endian order in addition to the QByteArray.

    I can do it with like a QList<quint16> filled by iterating every 2 indexes and reverse the byte order. That works

    But how can I link these 2 containers so that when I modify one the other is also changed and I can write it back to my file ?

    I do not know how I could make a reference to a data in the wrong endianness


  • Hi,
    from the logical point of view I'd subclass either QByteArray directly or QObject in a fashion that resulting class would emit the signal upon data change. I imagine this would require implementing separate method for adding/removing/replacing data (something simple, calling usual method for the data and then emitting the signal) plus similar subclassing for target where you could implement slot reacting to the change. And since signal/slot relation allows for passing the data you could pass along indexes affected to limit copying the data over.

  • I would have imagined a clever way to achieve this with data manipulation so I could still use iterators and so on, like that

    quint16& UInt16Array::operator[](int index){
    	quint8 a = _array[index * 2];
    	quint8 b = _array[index * 2 + 1];
    	qint16 value = (b << 8) | a;
    	QList::operator[](index) = value;
    	return value;

    But I have no control over the quint16 reference if gets modified it won't change the original _array.
    I could return a reference to an object I control, what do you think ?

Log in to reply