2015-06-29 13 views
6

Sto provando a creare QByteArray da std :: vector .. Ho provato;Come convertire std :: vector <uint8_t> in QByteArray?

std::vector<uint8_t> buf; 
QByteArray img = new QByteArray(reinterpret_cast<const char>(buf), buf.size()); 

Tuttavia dà errore;

error: invalid cast from type 'std::vector<unsigned char, std::allocator<unsigned char> >' to type 'const char' 

risposta

12

È necessario lanciare buf.data() invece di buf:

QByteArray* img = new QByteArray(reinterpret_cast<const char*>(buf.data()), buf.size()); 
+4

Nota '.data()' è disponibile soltanto in C++ 11 e versioni successive. Se il tuo compilatore non supporta questo, usa '& buf [0]'. – Saul

+0

quando uso buf.data() dice 'errore: cast da 'unsigned char *' a 'const char' perde precisione' – goGud

+0

ohh .. il mio errore, non ho usato il puntatore char .. grazie mille – goGud