Important: Please read the Qt Code of Conduct -

Something goes wrong with OpenCV in QtCreator

  • I want to use OpenCV in QtCreator and I edit the .pro file.When I use OpenCV 1.0 lib,code is showed below:

    •   IplImage* pFrame = NULL;
        CvCapture* capture = cvCreateCameraCapture(1);
        cvNamedWindow("video", 1);
        while (1){
            pFrame = cvQueryFrame(capture);
            if (!pFrame) break;
            cvShowImage("video", pFrame);
            char c = cvWaitKey(33);
            if(c == 27) break;
        return 0;

    It could open the camera and show it in a window.But when I use OpenCV 2.x lib,code is showed below:

    •  Mat pFrame;
       VideoCapture capture(1);
       if (!capture.isOpened()){
           cout << "open camera failed!";
           return -1;
       namedWindow("test", 1);
       while (1){
           capture >> pFrame;
           if (pFrame.empty()) break;
           imshow("test", pFrame);
           char c = cvWaitKey(33);
           if (c == 27) break;
       return 0;

    It just keeps creating new windows,though every window could show one image from the camera.
    But when I use VS2013 to do the samething, it all work out fine.
    Thank you for reading my question.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you using the exact same code in VS and Qt Creator ?

  • Yes,exactly the same. I just copy the code from VS to Qt Creator.

  • Lifetime Qt Champion

    Are you using the same project for Visual Studio and Qt Creator ?

  • No, just use the same code in different projects.

  • Lifetime Qt Champion

    Then are you using the same version of OpenCV for both projects ?

  • Of course,I use the same OpenCV lib for VS2013 and QtCreator. What do you think might go wrong?

  • Lifetime Qt Champion

    IIRC, OpenCV has a Qt backend so I was wondering if there was a mix somewhere that could trigger that bug

  • Could you send us the minimum projects which could reproduce the bugs(.hpp, .cpp and your .pro), if you could put it on github, it would be even better(easier for us to download and test it)
    I have a long time haven't played with openCV2, but I do have some experience about it

    By the way, since you are using Qt, why not just use QLabel or graphicsview framework to show the image?If you need to convert the QImage and cv::Mat, you could look at here.

Log in to reply