How to convert QVector<double> to QBytearray then display it via QPixmap



  • 0 down vote favorite

    I have big problem with load and display data. I will load data from .csv file to QVector than convert it to QBytearray and display it via QPixmap.

    Bellow I will show you a fiew steps.

    1. Load data from .csv to QVector
    2. Convert QVector to QBytearray.

    @
    QByteArray InfraredCamera::retFrameBArray(const int* frameNumber)
    {
    Frame temp = Pictures[frameNumber];
    QVector<double>
    vec = temp.retVecFrameValue();
    QByteArray arr = QByteArray::fromRawData( reinterpret_cast<const char*>(vec),(vec->size())*(sizeof(double)));

    for(int i = 0; i< vec->size(); i++)
        arr.append(reinterpret_cast<const char*>(temp.retFrameValue(&i)),sizeof(double));
    
    return arr;
    

    }
    @

    1. Than I will display it via QPixmap

      @
      int x = 3; //Chose numer of frame to display
      QByteArray arr = test->Termo.retFrameBArray(&x);
      QPixmap pic;
      pic.loadFromData(arr);@

    Then my program crashes.... What should I do with it?

    Bonus question: Would have been nice if I could show some of these images as a movie... Anyone know how you can do it?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    From a quick look at your code arr is pointing to an invalid position in memory.

    QByteArray::fromRawData explicitly state that there's no copy involved so you have to ensure that the underlying data lives at least as long as the QByteArray which is not the case in your code since temp gets destroyed at the end of retFrameBArray.

    On a side note, why are you using a pointer from frameNumber ? Same goes for vec, why a pointer to a QVector ? And why to you need vec since you are only using it to get its size ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.