如何将std::vector<uint8_t>转换为QByteArray?

How to convert std::vector<uint8_t> to QByteArray?

我正在尝试从 std::vector 创建 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'

您需要使用 buf.data() 而不是 buf:

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