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. How can I use the value(QByteArray) of one class in another?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
shared valueqbytearraysharedudp
9 Posts 3 Posters 3.0k 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.
  • ChristianMonteroC Offline
    ChristianMonteroC Offline
    ChristianMontero
    wrote on last edited by
    #1

    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!

    Q 1 Reply Last reply
    0
    • ChristianMonteroC ChristianMontero

      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!

      Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

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

      ChristianMonteroC 1 Reply Last reply
      0
      • Q qxoz

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

        ChristianMonteroC Offline
        ChristianMonteroC Offline
        ChristianMontero
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • ChristianMonteroC Offline
          ChristianMonteroC Offline
          ChristianMontero
          wrote on last edited by
          #4

          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

          Q 1 Reply Last reply
          0
          • ChristianMonteroC ChristianMontero

            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

            Q Offline
            Q Offline
            qxoz
            wrote on last edited by qxoz
            #5

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

            ChristianMonteroC 1 Reply Last reply
            0
            • Q qxoz

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

              ChristianMonteroC Offline
              ChristianMonteroC Offline
              ChristianMontero
              wrote on last edited by ChristianMontero
              #6

              @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

              Q 1 Reply Last reply
              0
              • DiracsbracketD Offline
                DiracsbracketD Offline
                Diracsbracket
                wrote on last edited by Diracsbracket
                #7

                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?

                ChristianMonteroC 1 Reply Last reply
                4
                • ChristianMonteroC ChristianMontero

                  @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

                  Q Offline
                  Q Offline
                  qxoz
                  wrote on last edited by
                  #8

                  @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));
                  
                  1 Reply Last reply
                  2
                  • DiracsbracketD Diracsbracket

                    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?

                    ChristianMonteroC Offline
                    ChristianMonteroC Offline
                    ChristianMontero
                    wrote on last edited by
                    #9

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

                    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