Solved

QT, Copy data from QDataStream to QByteArray

Posted on 2011-03-11
2
1,707 Views
Last Modified: 2012-05-11
Hi experts,

I have a QDataStream with data read from a file. Every 2 bytes in this file are a signed integer value.

How do I copy the data in the data stream to a QByteArray ?

For example:

QByteArray inputf1;
QDataStream in1(&filein1);

I need to copy the data from input in1 to inputf1.

Thanks,
Peter
0
Comment
Question by:PeterVarain
2 Comments
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
ID: 35107272
you could use resize function of QByteArray to get proper size and then cast off constness off from constData function to get a writeable char buffer.

  QByteArray inputf1;
  QDataStream in1(&filein1);
   inputf1.resize(4096);
   in1.readRawData(const_cast<char*)(inputf1.constData()), 4096);

but that is a bit unsafe because of the cast.

if memory isn't an issue i would use a temporary char buffer for read and move it to QByteArray using the insert function after read.

Sara



0
 

Author Closing Comment

by:PeterVarain
ID: 35114839
Thanks!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now