Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. creating and storing QByteArray from std::vector raw data

creating and storing QByteArray from std::vector raw data

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    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 (https://doc.qt.io/qt-5/qbytearray.html#QByteArray-1).
    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 *>(bytes.data()), writtenBytes);
    	m_queryBoundValues.append(baOffsets);
    
    jsulmJ 1 Reply Last reply
    0
    • U user4592357

      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 (https://doc.qt.io/qt-5/qbytearray.html#QByteArray-1).
      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 *>(bytes.data()), writtenBytes);
      	m_queryBoundValues.append(baOffsets);
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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"?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      U 1 Reply Last reply
      1
      • jsulmJ jsulm

        @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"?

        U Offline
        U Offline
        user4592357
        wrote on last edited by user4592357
        #3

        @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).

        jsulmJ 1 Reply Last reply
        0
        • U user4592357

          @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).

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @user4592357 Try to compare https://doc.qt.io/qt-5/qbytearray.html#length 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?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          U 1 Reply Last reply
          0
          • jsulmJ jsulm

            @user4592357 Try to compare https://doc.qt.io/qt-5/qbytearray.html#length 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?

            U Offline
            U Offline
            user4592357
            wrote on last edited by user4592357
            #5

            @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;
              }
            }
            
            1 Reply Last reply
            0
            • U Offline
              U Offline
              user4592357
              wrote on last edited by
              #6

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

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved