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!
qxoz last edited by
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
where I declared the QByteArray buffer as public, the in MyUDP.ccp i use it like this:
Then in my Window class I want to get acces to that value to print it in a QLabel, something like this:
qxoz last edited by qxoz
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?
ChristianMontero last edited by 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:
in the message recieved there will the de number (0/1) of output where I should print the value
Diracsbracket last edited by Diracsbracket
If you are sure the instance in your UDP class remains alive, and if you declare a
pointer to the
QByteArrayas a member of that class,
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_file->readData(filename); emit dataAvailable(*m_data);
Windowclass, 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?
qxoz last edited by
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!