Table Widget <- uint8_t *



  • Hi all,

    I've create a table widget to show data of packet which I receive with a raw socket.
    I want to insert the complete packet,which it has uint8_t * format, in one field.
    I've used this sentence to insert the data into the field.

     QTableWidgetItem(QString::asprintf((char *)data_packet)
    

    but when I try to get the data back , I don't get result I want.

    QTableWidgetItem *a;
    a=ui->Display_table->item(5,4);
    uint8_t *prueba;
    prueba=new uint8_t[IP_MAXPACKET];
    memcpy(prueba,a,sizeof(uint8_t)*IP_MAXPACKET);
    

    any help?

    Thanks in advance.


  • Qt Champions 2017

    @mgpmad said:
    Hi
    http://doc.qt.io/qt-5/qtablewidget.html#item
    it returns the QTableWidgetItem *
    So maybe you mean to take its text ?
    http://doc.qt.io/qt-5/qtablewidgetitem.html#text

    Its seems a bit messy to store data_packet as the caption.

    Each item can have data besides its text
    http://doc.qt.io/qt-5/qtablewidgetitem.html#setData
    Would be a safer way to store it.



  • thanks for your quick answer
    What I really want is to insert a int8_t * into a field of the table, and I want to get it back later into the same format int8_t *
    Is that possible?

    Regards


  • Qt Champions 2017

    @mgpmad
    Hi
    Yes it would be possible to store a pointer in the data section.
    But what does it point to?
    Later when you take it out again, the info it points to will still be valid?

    Could you show how data_packet is defined?



  • Sorry I express myself wrongly

    I don't want to store the pointer,rather the information that pointer points.

    uint8_t *data_packet;
    data_packet=new uint8_t[65535];
    sock_promis=socket(PF_PACKET, SOCK_RAW, htons (ETH_P_ALL));
    recvfrom (sock_promis, data_recv, IP_MAXPACKET, 0, (struct sockaddr *) &from, &fromlen);
    memset(data_packet,0,sizeof(uint8_t)*65535);
    memcpy(data_packet,data,sizeof(uint8_t)*65535);
    

  • Qt Champions 2017

    @mgpmad
    Ok. I understand.
    The Data member is QVariant and can store all types.
    But one need to register it.
    http://kunalmaemo.blogspot.dk/2010/11/storing-custom-class-in-qvariant.html
    Then you can use it directly.

    Alternatively you could a QByteArray
    it has constructor
    QByteArray(const char * data, int size)
    that would allow u to construct it easy from data_packet
    QByteArray is supported by QVariant directly and then
    the SetData and Data would just work.
    But to get a uint8_t[65535] back from it you would have to
    copy unless you can just use the QByteArray.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.