PDA

View Full Version : Obtain const QImage from QByteArray



mcosta
26th April 2011, 15:55
What do you means for "trying to obtain a const QIMage from a QByteArray"?

It depends to how QImage is stored in QByteArray.
If you want to use a "standard" way read here

squidge
26th April 2011, 18:27
What you have written will never work. socket->readAll() will only read and return what is available, which is unlikely to be an entire image. You need to keep reading each time you get a signal until you have received all the bytes and then attempt to turn it into an image. How you decide how many bytes are required is upto you, maybe you transmit it in the data, maybe it's fixed, that part is upto you.

gorsanmo
26th April 2011, 18:41
Hello :
Do you know where I can find any example of code for doing it ? Which method of QTCPSocket will I have to use ? Is the socket->readAll() not the correct method ?
Could you answer me ?
Thanks
GOrka

Fallen_
26th April 2011, 18:42
What you probably want is:
from server:

QByteArray arr;
QDataStream stream(&arr, QIODevice::WriteOnly);
stream.setVersion(QDataStream::Qt_4_7);

QImage image = ...;
stream << image;
then in client:

QDataStream stream(&some_socket, QIODevice::ReadOnly);
stream.setVersion(QDataStream::Qt_4_7);

QImage image = ...;
stream >> image;
The operators are in the documentation, >> and << for QImage
Hope this helps

gorsanmo
26th April 2011, 18:50
1.
QDataStream stream(&some_socket, QIODevice::ReadOnly);
2.
stream.setVersion(QDataStream::Qt_4_7);
3.

4.
QImage image = ...;
5.
stream >> image;


What would be the code for QImage ( QIMage= ... )
Thanks
GOrka

Fallen_
26th April 2011, 18:51
from the client should just be "QImage image;"

gorsanmo
26th April 2011, 20:38
when you use
QDataStream stream(&some_socket, QIODevice::ReadOnly);
stream.setVersion(QDataStream::Qt_4_7);

QImage image = ...;
stream >> image;
Does it mean that all the stream is kept in the stream ?

What does QImage image = ... mean ? DOes it mean that QImage image = new QImage ()?

gorsanmo
27th April 2011, 00:47
Hello :
Do you need to use in your TCP Client any kind of SIGNAL -- SLOT ?
For example , the one that I use here :

QTCPSocket socket = new QTCPSocket(this);
soket ->connectToHost("XXX","YYY");
connect(soket,SIGNAL(readyRead()),this,SLOT(leer_c aracter()))

Could you answer me as soon as possible ?
Thanks
gorsanmo

Added after 42 minutes:

As you have said , the code of the client is the following :


then in client:
QDataStream stream(&some_socket, QIODevice::ReadOnly);
stream.setVersion(QDataStream::Qt_4_7);

QImage image = ...;
stream >> image;
The operators are in the documentation, >> and << for QImage
Hope this helps

1) Is it supponed that in QDataStream(&some_socket, QIODevice::ReadOnly) , &some_socket is a QByteArray obtained reading an especific socket ?
For example

soket = new QTcpSocket(this);
soket->connectToHost("xxx","yyy");

connect(soket,SIGNAL(readyRead()),this,SLOT(leerca racter()));





leercaracter()
{
some_socket = soket->readall()
QDataStream(&some_socket, QIODevice::ReadOnly)
}

gorsanmo
27th April 2011, 09:46
Hello ,
what would be the best way for obtaining an Image by reading bytes from a QTCPSocket ?
Do you have any code example ?
Thanks