Important: Please read the Qt Code of Conduct -

image transfering[SOLVED]

  • hello,I'm studying about image transfering on Qt now.
    but it is difficult to finish making please help me.
    now I'm thinking the way below:
    [server side]
    1.first get image data of web cam by OpenCV's function. cvCaptureFromCam()
    2.change the image data to QByteArray by QByteArray(img->imageData, img->imageSize)
    3.add image's length and height data to QByteArray by append()
    4.transfer the QByteArray Data by writeDatagram()
    [client side]
    1.get data by readDatagram() length data and heidht data by QByteArray's function indexOf()
    3.change it to char pointer(char *) by
    char *cdata;
    cdata =; (store is QByteArray Data = image)
    4.change it to QImage by function(1)
    5.change it to QPixmap
    6.add it to Qlabel

    please,tell me how to do.

    QImage* MainWindow::char2QImage(char *cdata)
    int channels = 3;
    QImage *qimg = new QImage(width_data, height_data, QImage::Format_ARGB32);
    char *data = cdata;

    for (int y = 0; y < height_data; y++, data += width_data * channels)
        for (int x = 0; x < width_data; x++)
            char r, g, b, a = 0;
            if (channels == 1)
                r = data[x * channels];
                g = data[x * channels];
                b = data[x * channels];
            else if (channels == 3 || channels == 4)
                b = data[x * channels];
                g = data[x * channels + 1];
                r = data[x * channels + 2];
         if (channels == 4)
                a = data[x * channels + 3];
                qimg->setPixel(x, y, qRgba(r, g, b, a));
                qimg->setPixel(x, y, qRgb(r, g, b));
    return qimg;


  • Lifetime Qt Champion


    Why not directly send a QImage ? That would avoid having to play with raw data of unknown size

  • @SGaist
    Thank you for advising.

    I don't know the way to transfer the QImage data from server to client.
    so, I use this method.
    is it possible to transfer the QImage on UDPsocket communication?

  • Hi,

    You can use QDataStream to write/read a QImage on a QByteArray

  • Thank you for telling me.

    I will try to use the QDataStream.

  • Lifetime Qt Champion

    To add to @mcosta, if you don't want to risk packet loss, then you should use TCP so QTcpSocket/Server

  • Thank you for advising.
    I will try it.

Log in to reply