Important: Please read the Qt Code of Conduct -

[SOLVED]How to store short in a QByteArray?

  • Hi,

    I am having a vector of 'short' and i want to convert these 'short' into 'char', so as to be able to store them in a QByteArray.

    I am doing these because, i want to playback this data from the output device wich accept only QByteArray.

    Please help me to solve this issue.

    Thanx in advance.

  • convert the short to a const char * and store it in your QByteArray
    all the best

  • Something like this
    @QByteArray ba((const char*)&crc, sizeof(ushort));@

  • @qxoz, thanx for your reply.
    i will try this and let u know.
    thnx again.

  • hey i tried this...

    @QByteArray ba((const char*), sizeof(ushort));

    but i got this error...

    error: passing 'const QByteArray' as 'this' argument of 'QByteArray& QByteArray::append(const QByteArray&)' discards qualifiers [-fpermissive]

    and one thing my "playbackBuffer" is of type const QByteArray.

  • Ubuntu? add fpermissive to project CFLAGS:
    @QMAKE_CXXFLAGS = -fpermissive@
    don't forget to run qmake after editing .pro file

  • hey thanx for ur reply..

    but i dont want to use this flag, basically this flag is going to suppress the error which m getting and i want to fix my code.

    please help me with this..

  • this is not error, it's a warning... calling const instance functions is ok, but dangerous if you don't exactly know what are you doing, thats because gcc warns you about that.
    Ubuntu patched gcc has been modified to treat this warning as error... So, you will be just warned with vanilla gcc and compilation will success. It's all on you how to fix this issue...

  • so long the “playbackBuffer” is of type const QByteArray, you will getting this warning.

  • but i am getting this as an error not warning..
    so how do i fix it.

  • I've told you

    bq. Ubuntu patched gcc has been modified to treat this warning as error…

    @QMAKE_CXXFLAGS = -fpermissive@
    to project file or redesign your code...

  • hey thanx for the precious time u gave for my error...

    but i made it work without setting the flag....

    like this...

    @const QByteArray ba((const char*), sizeof(ushort));
    const_cast<QByteArray &>(playbackBuffer).append(ba);

    thanx once again...

    [EDIT: code formatting, Volker]

  • @QByteArray ba((const char*), sizeof(ushort));
    const_cast<QByteArray &>(playbackBuffer).append(ba);@

Log in to reply