Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Using QByteArray with C++ types

Using QByteArray with C++ types

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 3 Posters 1.3k 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.
  • M Offline
    M Offline
    maydin
    wrote on last edited by
    #1

    https://forum.qt.io/topic/66808/qt3d-draw-grid-axis-lines/3

    I want to define custom shapes in Qt3D using PyQt5. In this examples there is a code like this:

    QByteArray indexBytes;
    indexBytes.resize(2 * sizeof(unsigned int)); // start to end
    unsigned int *indices = reinterpret_cast<unsigned int*>(indexBytes.data());
    *indices++ = 0;
    *indices++ = 1;
    
    auto *indexBuffer = new Qt3DRender::QBuffer(geometry);
    indexBuffer->setData(indexBytes);
    

    I want to port this code to Python but I have no idea how to populate QByteArray with C++ types. If this is impossible is there a way to define custom mesh in PyQt3D?

    JonBJ 1 Reply Last reply
    0
    • JonBJ JonB

      @maydin
      So this code creates an 8-byte QByteArray, tells compiler to treat it as a pointer to a buffer of unsigned ints, and then pokes in two integers one after the other.

      If we ignore for the moment the question of integer storage byte-order, this looks similar-ish to memcpy(), so I Googled python memcpy (you might try others). This led me to https://stackoverflow.com/questions/13689628/is-there-a-python-equivalent-to-memcpy, and the accepted solution there. Untested, but I think they are saying you can go:

      indices[0:4] = 0
      indices[4:8] = 1
      

      This should certainly work if indices = bytes(8). I don't know if you can do that against a QByteArray. You can use a Python bytes where you can use a Qt QByteArray, you'd have to check whether conversion does a copy of whether it can use the Python bytes directly in the QByteArray.

      I hope the above gets you going at least.

      M Offline
      M Offline
      maydin
      wrote on last edited by
      #3

      @JonB Using struct.pack solved my problem. Here equivalent Python code:

      bufferBytes = QByteArray(struct.pack('6f', 0, 0, 0, 2, 2, 2))
      

      compared C++ one:

      QByteArray bufferBytes;
      bufferBytes.resize(3 * 2 * sizeof(float));
      float *positions = reinterpret_cast<float*>(bufferBytes.data());
      *positions++ = 0;
      *positions++ = 0;
      *positions++ = 0;
      *positions++ = 2;
      *positions++ = 2;
      *positions++ = 2;
      
      1 Reply Last reply
      3
      • M maydin

        https://forum.qt.io/topic/66808/qt3d-draw-grid-axis-lines/3

        I want to define custom shapes in Qt3D using PyQt5. In this examples there is a code like this:

        QByteArray indexBytes;
        indexBytes.resize(2 * sizeof(unsigned int)); // start to end
        unsigned int *indices = reinterpret_cast<unsigned int*>(indexBytes.data());
        *indices++ = 0;
        *indices++ = 1;
        
        auto *indexBuffer = new Qt3DRender::QBuffer(geometry);
        indexBuffer->setData(indexBytes);
        

        I want to port this code to Python but I have no idea how to populate QByteArray with C++ types. If this is impossible is there a way to define custom mesh in PyQt3D?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @maydin
        So this code creates an 8-byte QByteArray, tells compiler to treat it as a pointer to a buffer of unsigned ints, and then pokes in two integers one after the other.

        If we ignore for the moment the question of integer storage byte-order, this looks similar-ish to memcpy(), so I Googled python memcpy (you might try others). This led me to https://stackoverflow.com/questions/13689628/is-there-a-python-equivalent-to-memcpy, and the accepted solution there. Untested, but I think they are saying you can go:

        indices[0:4] = 0
        indices[4:8] = 1
        

        This should certainly work if indices = bytes(8). I don't know if you can do that against a QByteArray. You can use a Python bytes where you can use a Qt QByteArray, you'd have to check whether conversion does a copy of whether it can use the Python bytes directly in the QByteArray.

        I hope the above gets you going at least.

        M 1 Reply Last reply
        0
        • JonBJ JonB

          @maydin
          So this code creates an 8-byte QByteArray, tells compiler to treat it as a pointer to a buffer of unsigned ints, and then pokes in two integers one after the other.

          If we ignore for the moment the question of integer storage byte-order, this looks similar-ish to memcpy(), so I Googled python memcpy (you might try others). This led me to https://stackoverflow.com/questions/13689628/is-there-a-python-equivalent-to-memcpy, and the accepted solution there. Untested, but I think they are saying you can go:

          indices[0:4] = 0
          indices[4:8] = 1
          

          This should certainly work if indices = bytes(8). I don't know if you can do that against a QByteArray. You can use a Python bytes where you can use a Qt QByteArray, you'd have to check whether conversion does a copy of whether it can use the Python bytes directly in the QByteArray.

          I hope the above gets you going at least.

          M Offline
          M Offline
          maydin
          wrote on last edited by
          #3

          @JonB Using struct.pack solved my problem. Here equivalent Python code:

          bufferBytes = QByteArray(struct.pack('6f', 0, 0, 0, 2, 2, 2))
          

          compared C++ one:

          QByteArray bufferBytes;
          bufferBytes.resize(3 * 2 * sizeof(float));
          float *positions = reinterpret_cast<float*>(bufferBytes.data());
          *positions++ = 0;
          *positions++ = 0;
          *positions++ = 0;
          *positions++ = 2;
          *positions++ = 2;
          *positions++ = 2;
          
          1 Reply Last reply
          3
          • M Offline
            M Offline
            MarthaSimons
            wrote on last edited by MarthaSimons
            #4

            maydin thanks 👌

            1 Reply Last reply
            0

            • Login

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