Important: Please read the Qt Code of Conduct -

problem with QImage function

  • Hello,
    I am grabbing a frame from the camera and trying to save it. I am not getting the proper image. There are tiles in the image: example: if I take a picture of a pen, in the top 1/4th part of the saved image I can see 3 pens from left to right and rest of the image is black.
    Below is the code:

    int frmSize;
    unsigned char *frameData=NULL;

    //ISG_GetFrame syntax: ISG_GetFrame(int cameraNumber, unsigned char **frameData, int *frameSize)
    int camNum - value 0 to n used to select one of multiple cameras available. In the case
    of a single camera connection, value 0 should be used.
    unsigned char **frameData - pass the address location of the pointer that will be used to reference
    the returned image.
    int frameSize - takes the address of an integer that will hold the number of bytes in the image
    Return value:
    int status - returns 0 on success, -1 on timeout waiting for an available frame

    QImage img(frameData,1920,1200,QImage::Format_Mono);
    //img=img.convertToFormat(QImage::Format_RGB16); "C:\\test_folder\\fileTEST20.JPG" );

    I have checked that the camera is returning MONO8 image with width 1920 and height 1200

    Please help me with this issue. Thank You

  • Lifetime Qt Champion

    And what's your exact problem now?
    See ctor documentation: "The buffer must remain valid throughout the life of the QImage..."

  • @Christian-Ehrlicher Hello Christian,
    The buffer is valid till it is converted to QImage. Now the problem is that, I am trying to display QImage on QGraphicsview as a video and I am missing some frames. Below is the function that I am using:

    void MainWindow::whenNewFrame(const QImage& im)
    QPixmap stream_pix=QPixmap::fromImage(im);
    if (!stream_pix.isNull())
    delete ui->graphicsView->scene();
    ui->graphicsView->setScene(new QGraphicsScene(ui->graphicsView));

    So, I want to know if there is anything I can do to speed up the process.

  • Lifetime Qt Champion

    Not deleting the QGraphicsScene and QGraphicsPixmapItem on every call to whenNewFrame will be a good starting point...

  • @Christian-Ehrlicher if I remove the delete ui->graphicsView->scene() from the code then I got an error "QImage: out of memory, returning null image". I have read that addPixmap(stream_pix) will return a pointer to QGraphicsPixmapItem and I can update it instead of adding and deleting scene every time. But, I am not sure how to do that.

  • Lifetime Qt Champion

    @hardikgarg19 said in problem with QImage function:

    But, I am not sure how to do that.

    As member of the class for example.

  • @Christian-Ehrlicher
    I am able to solve the problem using setPixmap() function. Below is the code:


    QGraphicsPixmapItem *Item;

    inside the constructor:

    QImage defImg=QImage(1920,1200,QImage::Format_Grayscale8);
    QPixmap defStream_pix=QPixmap::fromImage(defImg);
    ui->graphicsView->setScene(new QGraphicsScene(ui->graphicsView));

    inside the constructor, a pixmap is created and added to the scene. "Item" is the returned pointer.
    now I have updated the "Item" whenever there is new image and update the scene.

    void MainWindow::whenNewFrame(const QImage& im)
        QPixmap stream_pix=QPixmap::fromImage(im);
        if (!stream_pix.isNull())

    Thank you @Christian-Ehrlicher

    /edit by moderator: added <code> tags

  • Lifetime Qt Champion

    Nice, then you can mark the topic as solved now :)

Log in to reply