Using QThread to display live camera feed

  • I am trying to write an application which takes frames from a web camera and displays them on screen. Since I will be processing the frames (using OpenCV, etc), I want to improve the speed of the application using threads. How can I do this? I believe it involves 2 steps: (i) fetching frames from the camera, and (ii) refreshing GUI to display them.

  • Hi prerna,

    This is quite a broad question, so I will try to give a general answer:

    You can create separate threads using:
    QThread* thread1 = new QThread;
    QThread* thread2 = new QThread;

    You can move objects without parents into the threads using:
    MyObj myObj = new MyObj(0); // 0 = no parent

    In your case, I am not sure how your project is setup, but you can create and put an "imageProcessing" object into a thread (e.g. called imageProcessingThread). Then to start that thread you can do the following to start it:

    QObject::connect(imageProcessingThread, SIGNAL(started()), imageProcessing, SLOT(run()));

    Note: that once the object is moved into the thread you should not call members of the object directly from outside the thread. Only use sockets and slots to communicate to it from any other thread or from the main application thread. So you may have to pass in your image data via socket / slot etc...

Log in to reply

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