2015-06-29 147 views
6

我想從std :: vector創建QByteArray ..我試過了;如何將std :: vector <uint8_t>轉換爲QByteArray?

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

但它給出了錯誤;

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

回答

12

你需要投buf.data(),而不是buf

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

注意'。數據()'只在C++ 11及更高版本。如果你的編譯器不支持這個,使用'&buf [0]'。 – Saul

+0

當我使用buf.data()它說'錯誤:從'無符號char *'轉換爲'const char'失去精度' – goGud

+0

ohh ..我的錯誤,我沒有使用字符指針..非常感謝 – goGud

相關問題