Important: Please read the Qt Code of Conduct -

[SOLVED] QImage exception

  • Dear Members,

    I am trying to display 8bit grayscale images on a QLabel to provide live video stream from a scientific camera.
    I am getting the same error time to time, sometimes the code runs correctly for 5 times in a row but then the attached
    error came(also you can see part of the code from where the exception comes) and I really stucked in find out why.

    screenshot link

    I am writing a multi threading GUI application so my first idea was it must be some
    simultanious access problem to resources but now I am not sure.

    Thank you in advance for you help!

    Best regards,

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    There's no link to the screen shot. In between and since you are doing multithreading with QImage, you could check the Mandelbrot example to see how they do the communication.

    Hope it helps

  • @SGaist Hi and thank you for you answer. The link is now working, I'll check the mandelbrot example and see if I can get the answer for my question.

  • I have checked the mandelbrot example but I don't think I have simultanious access problems because I also tried to do the image processing sequential so I defined flags which tells when the QBytearray is ready so it's content is only changing when image processing is finished. If you check the screenshot you can see the exception comes from deep down but I don't know why.

  • Lifetime Qt Champion

    Why are you allocating the QImage on the heap ?

  • @SGaist Because I didn't want to have static resources because the whole software could consume quite big amount of resources because it handles a 10 fast camera system and each camera could produce 20Hz live video stream over the network. Do you think the problem comes from dynamic allocation?

  • @SGaist I've just tested it with allocating QImage on the stack but the same error occurs after nearly the same measurement (8-10)
    I also tried to check if the QImage is not valid but it did not helped because it seems that it is valid always.

  • Lifetime Qt Champion

    What image format are you using ?

  • @SGaist I wanted to have RGB32 for displaying because as I read Indexed_8 is not supported for display. So as you can see the error occurs when qt trying to convert from Indexed_8 to RGB32 (Of course the problem could be elsewhere). Basically the images are coming from a scientific camera which has a 12bit grayscale sensor but of course I down scaled the data to 8bit long before the control enters to this method

  • Lifetime Qt Champion

    You misunderstood the documentation, you can't draw into a QImage in that format, but you can display it.

  • @SGaist Yes, you are right. That is what I tried for the first time but the images were displayed wrong in Indexed_8 format that is why I wanted to convert them to RGB32. But I don't think this is the source of my problem. I made 30 measurements again but without updating the QBytearray so the data was always the same and the previous error never came. In my opinion somehow my image data could be wrong sometimes and that is why the conversion fails.

  • Lifetime Qt Champion

    Did you find what triggers the error in your data ? Where does it come from ?

  • @SGaist Not yet but It is strange that the error always came when the first frame arrives. I'll need some more time to debug.

  • Lifetime Qt Champion

    Only on the first frame ?

  • @SGaist Exactly. After the video streaming starts the error never shows up. I'll try to dump the first frame on the sender side and see what could be wrong with the data and so on. I don't think the problem caused by the QTcpSocket which transfers the frames.

  • @SGaist Finally It seems I could solve the problem. The problem was that my frame receiver side had some issues that caused that unexpected frames could come before the previous transfer finished and that caused that during image conversion the QByteArray that holds the image data could change thus read access violations occurred. After I solved this issue the application have not crashed ever since (~50 measurements).

    Thank you very much for your help! The conversations made me thinking more and finally this bug could be eliminated.


  • Sir, i am new to QT. So help me to fix this as i want to do the samething like you. I want to show live streming through i am using QTCreater 3.2.2 and QT 4.8 on fedora 20.
    sir, i have done recording video and capturing video by using following code. I just call this code by using slots of "capture image" and "record a video":
    // For Video:-
    system("ffmpeg -f video4linux2 -i /dev/video0 -vcodec mpeg4 -b 12000k -r 25 -t 00:00:55 test12.avi");

    //For image capture:-
    system("fswebcam -s 15 --jpeg 95 --save /abasfc.jpeg");

    Sir, i just want view this particular item on GUI (On QLabel or something) at the time of capturing image and recording video.. How to do that?
    thanks in advance...

  • Lifetime Qt Champion

    @Suraj, please don't hijack other users thread. You already have a thread opened here with answers

  • @SGaist ok sir. Sorry Sir. and thank you for guidance.

  • Thank all for your replay,
    I solve my problem by using Phonon: :video player


    i. Add Phonon::Video Player in ui file. I rename its programing name as "Video Player"
    ii. Add one button in that ui file and go to that button slot and adding following code in it:-
    // For getting system date and time for video name
    time_t now = time(0);
    struct tm tstruct;
    char buf[80];
    tstruct = *localtime(&now);

    strftime(buf, sizeof(buf), "%d_%m_%y_%H_%M", &tstruct);
    qDebug() << "Date and Time: "<<buf;

    // For getting static path where video will be stored
    QString a = "/home/Working_QT/BackupErrorWorking/DeviceTestVersion1/video/";
    qDebug() << "a: "<<a;

    // For giving comlete command as it is accepted as a QString and converted into char(Because konsole command is accepted only char it shows error so we convert it)
    QString command = "ffmpeg -f video4linux2 -i /dev/video0 -vcodec mpeg4 -b 500k -r 25 -t 00:01:30 ";
    qDebug() << "Command: "<<command;
    char x[200];
    strcpy( x, command.toStdString().c_str());

    // Commaand to sysytem
    qDebug() << "Command given to system to capture video and name it according to that particular date and time: "<<x;

    // For playing video through Phonon VideoPlayer
    qDebug("Before video recording display");
    qDebug("After video recording display");

    iii. Included #include <time.h> // Required for giving video file name according to date and time
    and #include <qdebug.h>
    iv. Make changes in .pro file

    QT += phonon