Qt World Summit: Register Today!

[SOLVED][ERROR] [HELP] Using Qt + OpenCV to get what does webcam see

  • I am using qt4.8 , raspbian wheezy
    i get some error and i not really understand.

    in dialog.ui, i just add a Label name "view" and 2 pushButton "startButton" & "stopButton"

    I will post my error msg after my coding.

    Please Help!
    Thanks in advance.

    #ifndef CAMERADEVICE_H
    #define CAMERADEVICE_H

    #include <QtCore/QObject>

    class QTimer;
    class QImage;

    namespace cv{
    class VideoCapture;
    class Mat;

    class CameraDevice : public QObject
    explicit CameraDevice(QObject *parent = 0);

    void imageReady(const QImage& image);

    public slots:
    bool start();
    bool stop();

    private slots:
    void onTimeout();

    QImage imageFromMat(const cv::Mat& frame);
    cv::VideoCapture * m_capture;
    QTimer * m_timer;

    #endif // CAMERADEVICE_H@

    #include <QtCore/QTimer>
    #include <QtGui/QImage>
    #include "opencv/cv.h"
    #include "opencv/highgui.h"
    #include "cameradevice.h"

    CameraDevice::CameraDevice(QObject *parent) :
    m_capture = new cv::VideoCapture;
    m_timer = new QTimer(this);

    connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimeout()));


    delete m_capture;
    m_capture = NULL;

    bool CameraDevice::start()
    if (m_capture->isOpened()) {
    return true;
    if (m_capture->isOpened()) {
    return m_capture->isOpened();

    bool CameraDevice::stop()
    if (m_capture->isOpened()) {
    return true;

    void CameraDevice::onTimeout()
    if (!m_capture->isOpened()) {
    static cv::Mat frame;
    *m_capture >> frame;
    if (frame.cols) {
    emit imageReady(imageFromMat(frame));

    QImage CameraDevice::imageFromMat(const cv::Mat &frame)
    const unsigned char* src = frame.ptr();
    QImage image(frame.cols, frame.rows, QImage::Format_RGB32);
    unsigned char * des = image.bits();
    unsigned long count = frame.cols * frame.rows;
    for (unsigned long i = 0; i < count; ++i)
    *des++ = *src++;
    *des++ = *src++;
    *des++ = *src++;
    *des++ = 0xff;
    return image;

    #ifndef DIALOG_H
    #define DIALOG_H

    #include <QtGui/QDialog>

    namespace Ui {
    class Dialog;

    class CameraDevice;

    class Dialog : public QDialog

    explicit Dialog(QWidget *parent = 0);

    private slots:
    void onImageArrival(const QImage & image);

    Ui::Dialog *ui;
    CameraDevice * m_camera;

    #endif // DIALOG_H@

    @#include <QtCore/QThread>
    #include "dialog.h"
    #include "ui_dialog.h"
    #include "cameradevice.h"

    Dialog::Dialog(QWidget *parent) :
    ui(new Ui::Dialog),
    m_camera(new CameraDevice)

    QThread * thread = new QThread(this);
    connect(m_camera, SIGNAL(imageReady(QImage)), this, SLOT(onImageArrival(QImage)));
    connect(ui->startButton, SIGNAL(clicked()), m_camera, SLOT(start()));
    connect(ui->stopButton, SIGNAL(clicked()), m_camera, SLOT(stop()));


    delete ui;

    void Dialog::onImageArrival(const QImage &image)

    #include <QtGui/QApplication>
    #include "dialog.h"

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    Dialog w;

    return a.exec&#40;&#41;;


    @pi@raspberrypi:~/qt/Dialog$ make
    g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -o cameradevice.o cameradevice.cpp
    g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -o dialog.o dialog.cpp
    dialog.cpp: In constructor ‘Dialog::Dialog(QWidget*)’:
    dialog.cpp:11:21: error: no matching function for call to ‘Ui::Dialog::setupUi(Dialog* const)’
    dialog.cpp:11:21: note: candidate is:
    ui_dialog.h:39:10: note: void Ui_Dialog::setupUi(QMainWindow*)
    ui_dialog.h:39:10: note: no known conversion for argument 1 from ‘Dialog* const’ to ‘QMainWindow*’
    make: *** [dialog.o] Error 1@

    please help, thank you!

  • Lifetime Qt Champion


    Did you start you project from a QMainWindow and then changed manually the class to a QDialog ?

  • Hi.
    For dialog I din change. But I change the cameradevice to QObject

  • Lifetime Qt Champion

    How did you create your dialog ?

  • Qt GUI application. Qmainwindow

  • Lifetime Qt Champion

    Then how did it become a QDialog ?

  • errr, I changed the QMainwindow to QDialog manually, i should use QMainwindow or QDialog to create Dialog class?

  • [UPDATE]
    After correction, I get another error as following:

    @pi@raspberrypi:~/qt/Dialog$ qmake -project
    pi@raspberrypi:~/qt/Dialog$ qmake Dialog.pro
    pi@raspberrypi:~/qt/Dialog$ make
    g++ -Wl,-O1 -o Dialog cameradevice.o dialog.o main.o moc_cameradevice.o moc_dialog.o -L/usr/lib/arm-linux-gnueabihf -lQtGui -lQtCore -lpthread
    cameradevice.o: In function CameraDevice::CameraDevice(QObject*)': cameradevice.cpp:(.text+0x24): undefined reference tocv::VideoCapture::VideoCapture()'
    cameradevice.o: In function cv::Mat::~Mat()': cameradevice.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x70): undefined reference tocv::fastFree(void*)'
    cameradevice.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x7c): undefined reference to `cv::Mat::deallocate()'
    collect2: ld returned 1 exit status
    make: *** [Dialog] Error 1@

  • The error is because the CameraDevice class is unable to find the cv class when calling it's static member function: VideoCapture. What type of library do you include? Or do you use the QLibrary for that??

  • i include

    1. highgui
    2. cv
    3. cxcore

  • From my knowledge,

    VideoCapture is under highgui.hpp.

    i have include this file already.

    anyone knwo what is happening?

  • Lifetime Qt Champion

    You are not linking to the OpenCV libraries

  • Do you mean in XXX.pro file ?

  • Lifetime Qt Champion

    Yes indeed, in you project pro file

  • @OPENCV_PATH = /usr/local/include/opencv2/
    LIBS_PATH = /usr/local/include/opencv/


    This is how my linking look like

  • Lifetime Qt Champion

    Are you sure you are linking to all the needed libraries ?

  • no, i am nt sure, but i checked many times if i left out any.
    Should not be left out...
    if this is not left out of libraries, what is the problem ?

  • Lifetime Qt Champion

    Just a missing library. You can check with pkg-config what you need. You can even use it within your pro file (have a look at qmake's documentation)

  • Ok. I give it a try and looh. Thx a lot SGaist

  • [quote author="SGaist" date="1403731430"]Just a missing library. You can check with pkg-config what you need. You can even use it within your pro file (have a look at qmake's documentation) [/quote]

    Don't really know how to do....
    I just have a look at the qmake genarated makefile. The INCPATH inside should be the openCV path ? It is different.
    the another thing to mention is, after i qmake project, my Dialog.pro file will changed. the coding will all replaced.

    i am new to Qt and OpenCV, i just could not link them up.

    Thanks to SGaist again,

  • Hi SGaist,

    Finally I did it!

    I make a slightly change in "qmake.conf" file which is under @/usr/share/qt4/mkspecs/default/qmake.conf@

    I added in the INCLUDEPATH and LIBS.
    Then it Works!!

  • Lifetime Qt Champion

    Don't modify that file, that will make all your projects link to OpenCV. Just correct your pro file with what you added there.

  • Hi SGaist,

    I don't get what you mean,
    Showing what camera get is only a part i need to do,

    I need openCV in my whole project, I need to do a Robot Vision System GUI,
    so, I still not suggested to modify that file?

  • Lifetime Qt Champion

    Modifying /usr/share/qt4/mkspecs/default/qmake.conf means that every new project you'll create will be linked to OpenCV which is not a good idea.

    Just move what you added in qmake.conf in your project's pro file

  • Dear YDYD sir,
    i am new in QT. I also want to start a camera and want a image capturing and video recording functionality. I already triend the example which is given on official webside. But it showing multiple errors in header file. consider there are two header file. first is "one.h" and second is "second.h". In one.h,second.h is included. One.h is included in my application. At first it gives error for one.h. For solving this error i locate that file and add it in "#include </path/one.h>" then it solve the problem. But it shows error for second.h and header files which is included in that. One time i add path for all this error. It solves it but it then shows error in the functions of this header file. Sir i dont know what to do now. So can you send me the demo application to me to my mail id "dangat123suraj@gmail.com" and suggest solution for this problem. Thanking you in advance.

Log in to reply