Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Link QByteArray to another container with different datatype
Forum Update on Monday, May 27th 2025

Link QByteArray to another container with different datatype

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 198 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • moffa13M Offline
    moffa13M Offline
    moffa13
    wrote on last edited by
    #1

    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

    Thanks

    1 Reply Last reply
    0
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      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.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      -1
      • moffa13M Offline
        moffa13M Offline
        moffa13
        wrote on last edited by
        #3

        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 ?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved