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.
-
-
@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? -
ChristianMonteroreplied to qxoz on 4 Oct 2017, 06:19 last edited by ChristianMontero 10 Apr 2017, 06:21
@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:
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 theQByteArray
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!
1/9