How can I use the value(QByteArray) of one class in another?



  • Hi, Good night
    I'm making a project in QT, and I have two classes, one that stablish a connection via UDP between 2 devices to send and recieve data, this data it's stored in a QByteArray and the other class it's call Window, and it's where I designed the GUI and I have an array of labels where i want to show the value of the QByteArray recieved by the UDP connection, how can I get acces to this value?

    Any help would be appreciated
    Thanks for your time!



  • @ChristianMontero
    are those classes related somehow?
    First coming to mind is using signal-slots. But if data is big you can use pointers or references.



  • @qxoz yeah, they're related, give me a second to include part of the code, please.



  • This is MyUDP.h
    0_1507096581912_16bb97ec-e85b-4d51-a07a-8bc05f6e05c1-image.png
    where I declared the QByteArray buffer as public, the in MyUDP.ccp i use it like this:
    0_1507096734138_5e349bfa-bf3c-406b-a415-c9d00be4a6f2-image.png `
    Then in my Window class I want to get acces to that value to print it in a QLabel, something like this:
    0_1507096948374_9cc4e9a0-70f3-4f6c-96f2-c13e237fe911-image.png



  • @ChristianMontero
    It is not clear to me now.
    Do you have problem with access to MyUDP object or updating when new data comes?
    Where do you create MyUDP object?



  • @qxoz I want to create an instace of MyUDP in my window.cpp
    and there I want to check te value of the buffer and print the value of each bit in an array of buttons, something like this:
    0_1507097947101_76f4472b-9b81-4ffa-ad0c-12470d09df7e-image.png

    in the message recieved there will the de number (0/1) of output where I should print the value



  • If you are sure the instance in your UDP class remains alive, and if you declare a
    pointer to the QByteArray as a member of that class,

    QByteArray*     m_data;
    

    Then you should be able to define a signal like this for that class:

    signals:
         void dataAvailable(const QByteArray& data);
    

    and emit it as follows after filling the byte array and storing its pointer in m_data

    m_data = m_file->readData(filename);
    emit dataAvailable(*m_data);
    

    In your Window class, you can connect that signal to a slot, e.g.

    public slots:
      void handleData(const QByteArray& data);
    

    In there, you can access the data as follows:

    QByteArray* dataArray = (QByteArray*)&data;
    //... do your stuff with dataArray
    

    This should work, no?



  • @ChristianMontero
    If you want show bits, you can convert QByteArray to QBitArray and use it:

    QByteArray bytes = ...;
     
    // Create a bit array of the appropriate size
    QBitArray bits(bytes.count()*8);
     
    // Convert from QByteArray to QBitArray
    for(int i=0; i<bytes.count(); ++i)
        for(int b=0; b<8; ++b)
            bits.setBit(i*8+b, bytes.at(i)&(1<<b));
    


  • @Diracsbracket I think it should, let me try it, thanks you so much for your time!



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