Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QByteArray dont working correctly for returned QImage
Forum Update on Monday, May 27th 2025

QByteArray dont working correctly for returned QImage

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 270 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AliPanahi2
    wrote on last edited by AliPanahi2
    #1

    QByteArray don't working correctly when use Qimage returned from other function;

    we have simple ImageProvider class:

    ImageProvider::ImageProvider(QObject *parent) : QObject(parent)
    {
        image = QImage("D:/01.bmp"); // monochrome image
    }
    
    QImage ImageProvider::getImage()
    {
        return image;
    }
    

    and ImageConverter class:

    ImageConverter::ImageConverter(QObject *parent) : QObject(parent)
    {
        imgProvider = new ImageProvider(this);
    }
    
    void ImageConverter::convert()
    {
        QByteArray bytesArray= QByteArray::fromRawData(
                             (const char*)imgProvider->getImage().bits(),
                                          imgProvider->getImage().byteCount());
    
        QByteArray bytesStream;
        QDataStream ds(&bytesStream, QIODevice::ReadWrite);
        ds.writeRawData((const char*)imgProvider->getImage().bits(),
                                     imgProvider->getImage().byteCount());
    
        qDebug() << "length : " << bytesArray.length() << " = " << bytesStream.length();
        for (int i = 0; i < bytesArray.length(); ++i) {
            if (bytesArray.at(i) != bytesStream.at(i)) {
                qDebug() << i << " : " << (int)bytesArray.at(i) << " = "
                                       << (int)bytesStream.at(i) << " = "
                                       << (int)imgProvider->getImage().bits()[i];
            }
        }
    }
    

    Output of program:

    98304  -  98304
    0  :  -60  =  0  =  0
    2  :  -105  =  0  =  0
    3  :  58  =  0  =  0
    4  :  -32  =  0  =  0
    5  :  52  =  0  =  0
    6  :  -10  =  0  =  0
    7  :  59  =  0  =  0
    

    why QByteArray don't working correctly for some bytes ?

    • Now if change
    QImage ImageProvider::getImage()
    

    to const

    const QImage ImageProvider::getImage()
    

    OR

    • if in function
    void ImageConverter::convert()
    

    add

    QImage img = imgPro->getImage();
    

    and use img instead imgProvider->getImage()
    QByteArray working correctly.

    why QDataStream working correctly anyway ?

    aha_1980A 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Because you are using a pointer to the internal data of a temporary variable.
      The correct way to do this is:

      QByteArray bytesArray
      QBuffer buff(&bytesArray);
      if(buff.open(QIODevice::WriteOnly))
          imgProvider->getImage().save(&buff,"PNG");
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      6
      • A AliPanahi2

        QByteArray don't working correctly when use Qimage returned from other function;

        we have simple ImageProvider class:

        ImageProvider::ImageProvider(QObject *parent) : QObject(parent)
        {
            image = QImage("D:/01.bmp"); // monochrome image
        }
        
        QImage ImageProvider::getImage()
        {
            return image;
        }
        

        and ImageConverter class:

        ImageConverter::ImageConverter(QObject *parent) : QObject(parent)
        {
            imgProvider = new ImageProvider(this);
        }
        
        void ImageConverter::convert()
        {
            QByteArray bytesArray= QByteArray::fromRawData(
                                 (const char*)imgProvider->getImage().bits(),
                                              imgProvider->getImage().byteCount());
        
            QByteArray bytesStream;
            QDataStream ds(&bytesStream, QIODevice::ReadWrite);
            ds.writeRawData((const char*)imgProvider->getImage().bits(),
                                         imgProvider->getImage().byteCount());
        
            qDebug() << "length : " << bytesArray.length() << " = " << bytesStream.length();
            for (int i = 0; i < bytesArray.length(); ++i) {
                if (bytesArray.at(i) != bytesStream.at(i)) {
                    qDebug() << i << " : " << (int)bytesArray.at(i) << " = "
                                           << (int)bytesStream.at(i) << " = "
                                           << (int)imgProvider->getImage().bits()[i];
                }
            }
        }
        

        Output of program:

        98304  -  98304
        0  :  -60  =  0  =  0
        2  :  -105  =  0  =  0
        3  :  58  =  0  =  0
        4  :  -32  =  0  =  0
        5  :  52  =  0  =  0
        6  :  -10  =  0  =  0
        7  :  59  =  0  =  0
        

        why QByteArray don't working correctly for some bytes ?

        • Now if change
        QImage ImageProvider::getImage()
        

        to const

        const QImage ImageProvider::getImage()
        

        OR

        • if in function
        void ImageConverter::convert()
        

        add

        QImage img = imgPro->getImage();
        

        and use img instead imgProvider->getImage()
        QByteArray working correctly.

        why QDataStream working correctly anyway ?

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi @AliPanahi2,

        citing the documentation of QByteArray::fromRawData():

        The bytes are not copied. The QByteArray will contain the data pointer. The caller guarantees that data will not be deleted or modified as long as this QByteArray and any copies of it exist that have not been modified.

        In your call, that is indeed not guaranteed, if you don't store the result of imgProvider->getImage() in a local variable.

        Regards

        Qt has to stay free or it will die.

        1 Reply Last reply
        4

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved