Results 1 to 6 of 6

Thread: convert uchar* to QbyteArray

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default convert uchar* to QbyteArray

    I get the screen raw data as uchar* as below:
    Qt Code:
    1. void MainWindow::getScreenShot()
    2. {
    3. QPixmap originalPixmap = QPixmap::grabWidget(this);
    4. QImage *image = new QImage(originalPixmap.toImage());
    5.  
    6. QSize imageSize = image->size();
    7. int len = imageSize.width() * imageSize.height() * 3; // ?????????
    8. uchar *rawData = new uchar[len];
    9.  
    10. emit sendRawDataToClient(rawData);
    11. }
    To copy to clipboard, switch view to plain text mode 

    then send it to client, now client needs to convert it to qbytearray, write() get char* and not uchar*, does it make any difference to send it as char* and not uchar*?
    Qt Code:
    1. void Client::rcvRawDataFromMW(uchar* rawData)
    2. {
    3. QByteArray block((char*)rawData);
    4. tcpSocket->write(block);
    5. tcpSocket->disconnectFromHost();
    6. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: convert uchar* to QbyteArray

    I would just save it to a buffer, this way you can even make use of a more efficient format, like PNG

    http://qt-project.org/doc/qt-4.8/qimage.html#save-2

    Or even directly into the TcpSocket, it is a QUIDevice after all

    Btw, in you example you are leaking the image data twice!
    Once because you needlessly create QImage on the heap and forget to delete it and the second time when you forget to delete rawData after emit.

    And for calculating "len" see QImage::byteCount()

    Cheers,
    _

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: convert uchar* to QbyteArray

    If sent over the network using Qt mechanisms then the receiver can trivially access the received data as a QByteArray: QIODevice::read().

    Nothing of value is being sent as your sendRawDataToClient() signal parameter. At line 8 you allocate a chunk of memory containing 'len' unsigned chars and send a pointer to that chunk of memory to any slot connected to the signal. Nothing initialises the memory and nothing connects it in any way to the image you are manipulating. Qt will dutifully send those bytes through the network assuming you don't make other mistakes handling it in the connected slot (we cannot see). The other end might receive this but it will not be useful.

  4. #4
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: convert uchar* to QbyteArray

    Quote Originally Posted by anda_skoa View Post
    I would just save it to a buffer, this way you can even make use of a more efficient format, like PNG
    Qt Code:
    1. bool QImage::save ( QIODevice * device, const char * format = 0, int quality = -1 ) const
    To copy to clipboard, switch view to plain text mode 

    I need raw data in QByteArray and not in any image format. As I can guess, the default value of format would save it in raw data without any format wouldn't it?

    Cheers,


    Added after 4 minutes:


    Quote Originally Posted by ChrisW67 View Post
    If sent over the network using Qt mechanisms then the receiver can trivially access the received data as a QByteArray: QIODevice::read().

    Nothing of value is being sent as your sendRawDataToClient() signal parameter. At line 8 you allocate a chunk of memory containing 'len' unsigned chars and send a pointer to that chunk of memory to any slot connected to the signal. Nothing initialises the memory and nothing connects it in any way to the image you are manipulating. Qt will dutifully send those bytes through the network assuming you don't make other mistakes handling it in the connected slot (we cannot see). The other end might receive this but it will not be useful.
    Yes, you are correct. Even it would be worse if I delete the image variable memory space. The good way is to define the variable inside the class.
    Last edited by saman_artorious; 13th August 2013 at 09:03.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: convert uchar* to QbyteArray

    Quote Originally Posted by saman_artorious View Post
    I need raw data in QByteArray and not in any image format.
    QImage::bits() returns the raw data in format specified by QImage::format()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: convert uchar* to QbyteArray

    I need raw data in QByteArray and not in any image format.
    What is "raw data"? The bytes representing the image must be arranged in some known way, a "format", in order to be at all useful.

Similar Threads

  1. Replies: 2
    Last Post: 19th July 2018, 06:38
  2. how to convert an int to QByteArray
    By babu198649 in forum Qt Programming
    Replies: 12
    Last Post: 19th September 2014, 09:47
  3. convert QString to QByteArray
    By morgana in forum Qt Programming
    Replies: 5
    Last Post: 2nd March 2011, 13:33
  4. Convert a QByteArray to a QVariantList
    By franco.amato in forum Newbie
    Replies: 21
    Last Post: 13th May 2010, 00:17
  5. how can we convert QByteArray to Qstring?
    By learning_qt in forum Qt Programming
    Replies: 2
    Last Post: 21st July 2009, 14:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.