Important: Please read the Qt Code of Conduct -

creating and storing QByteArray from std::vector raw data

  • i'm creating a QByteArray from std::vector's raw data, and storing the byte array in variant list.
    QByteArray docs say deep copy of data is made (
    but when i print the byte array when it is stored and when it is used, i get different bytes.

    this is my code, am i doing something incorrectly?

    	std::vector<unsigned char> bytes;
    	auto writtenBytes = encodePoints(points, bytes);
    	QByteArray baOffsets(reinterpret_cast<const char *>(, writtenBytes);

  • Lifetime Qt Champion

    @user4592357 said in creating and storing QByteArray from std::vector raw data:

    but when i print the byte array when it is stored and when it is used

    How do you print and what do you mean by "stored" and "used"?

  • @jsulm

    std::cout << baOffsets.toStdString() << std::endl;

    i mean, i store the byte array in m_queryBoundValues, then read it from there (in another function, so i think vector data is deleted by then).

  • Lifetime Qt Champion

    @user4592357 Try to compare instead of printing as string (0 bytes are interpreted as end-of-string).
    Also, how do you use and print m_queryBoundValues?
    Do you have an example of what is printed for both?

  • @jsulm
    okay, m_queryBoundValues is QVariantList, so i'm actually converting byte array to variant while storing it.
    i'm using the byte array as QSql::ParamType(QSql::In | QSql::Binary) type bound value for sql query, but i just checked there's no variant to byte array conversion...

    i was also getting this error, so i'm thinking the lack of conversion may be the problem?

    ~$ c++filt _ZN8QVariantaSERKS_
    QVariant::operator=(QVariant const&)

    i have a nested data structure:

    // type aliases
    using tQueryBoundValues = QVariantList; // bound values for one query
    using tQueryValues2Type = QPair<tQueryBoundValues, QSql::ParamType>;
    using tAllQueriesBoundValues = QList<tQueryValues2Type>; // bound values for list of queries
    using tTable2AllQueriesValues = QHash<QString, tAllQueriesBoundValues>; // bound values for all queries
    using tQuery2BoundData = QHash<EQuery, tTable2AllQueriesValues>;
    tQuery2BoundData m_mapQuery2BoundValues;

    this is the usage:

    for (EQuery queryType : m_mapQuery2BoundValues.keys())
      QString sQueryPattern = pQueries->get(queryType);
      const auto &queryTableDataMap = m_mapQuery2BoundValues[queryType];
      for (const QString &sTable : queryTableDataMap.keys())
        m_sqlQuery.prepare(sTable.isEmpty() ? sQueryPattern : sQueryPattern.arg(sTable));
        for (const auto &boundValueType : queryTableDataMap[sTable])
          m_sqlQuery.addBindValue(QVariant::fromValue(boundValueType.first), boundValueType.second);
        if (!m_sqlQuery.execBatch())
          return err = makeErrorString(m_sqlQuery.lastError().text()), false;

  • @jsulm
    nevermind, i found what i was missing, i bound less parameter than i actually had in my query, so i got that error.

Log in to reply