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?

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.
  • C Offline
    C Offline
    ChristianMontero
    wrote on 4 Oct 2017, 05:21 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 4 Oct 2017, 05:34
    0
    • C ChristianMontero
      4 Oct 2017, 05:21

      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 4 Oct 2017, 05:34 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.

      C 1 Reply Last reply 4 Oct 2017, 05:45
      0
      • Q qxoz
        4 Oct 2017, 05:34

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

        C Offline
        C Offline
        ChristianMontero
        wrote on 4 Oct 2017, 05:45 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
        • C Offline
          C Offline
          ChristianMontero
          wrote on 4 Oct 2017, 06:02 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 4 Oct 2017, 06:13
          0
          • C ChristianMontero
            4 Oct 2017, 06:02

            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 4 Oct 2017, 06:13 last edited by qxoz 10 Apr 2017, 06:15
            #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?

            C 1 Reply Last reply 4 Oct 2017, 06:19
            0
            • Q qxoz
              4 Oct 2017, 06:13

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

              C Offline
              C Offline
              ChristianMontero
              wrote on 4 Oct 2017, 06:19 last edited by ChristianMontero 10 Apr 2017, 06:21
              #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 4 Oct 2017, 06:54
              0
              • D Offline
                D Offline
                Diracsbracket
                wrote on 4 Oct 2017, 06:38 last edited by Diracsbracket 10 Apr 2017, 06:38
                #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?

                C 1 Reply Last reply 4 Oct 2017, 07:06
                4
                • C ChristianMontero
                  4 Oct 2017, 06:19

                  @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 4 Oct 2017, 06:54 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
                  • D Diracsbracket
                    4 Oct 2017, 06:38

                    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?

                    C Offline
                    C Offline
                    ChristianMontero
                    wrote on 4 Oct 2017, 07:06 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

                    7/9

                    4 Oct 2017, 06:38

                    • Login

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