0

I'm a beginner in using Qt and OpenCV, and I have a small problem. My application works fine, but after closing it seems that opencv.exe (application name) is still in memory. Here is my code:

int main(int argc, char* argv[]) {
    QCoreApplication a(argc, argv);
    cv::Mat img = cv::imread("img.jpg");
    cv::namedWindow("Image");
    cv::imshow("Image",img);
    return a.exec();
}

How to kill task with closing application window? I don't sure that I work correct with exec() function.

Everv0id
  • 1,862
  • 3
  • 25
  • 47
  • Thanks everyone, I found a good solution: http://stackoverflow.com/questions/4180394/how-do-i-create-a-simple-qt-console-application-in-c – Everv0id Jul 08 '13 at 21:16

3 Answers3

1

QCoreApplication::exec() starts an event loop.

Often times this is tied to the presence of a terminal window.

With QApplication::exec() it also starts an event loop, but it usually is tied to the presence of a QMainWindow or the last QWidget that was opened.

The easiest way right now for you to close it, is to go to Projects > Run > Run in Terminal, and check it.

You may also need to go to your .pro file and add CONFIG += console.

When you start using Qt signals and slots, the event loop will be extremely useful.

Also for any of Qt's GUIs to function properly you need the exec() event loop running.

Another way that you can kill your task when running it in Qt Creator is to go to the Application Output tab at the bottom and click the red square stop button.

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
0

You could try to call qApp->quit() in the close event of your non-qt window (I don't know OpenCV though).

qApp is equivalent to QCoreApplication::instance() if you started a non-gui application (in Qt terms of course), or a QApplication if you started a gui application.

azf
  • 2,179
  • 16
  • 22
0

To gracefully come out of event loop started by QCoreApplication::exec() QCoreApplication::quit () must be called. Somehow when you are done with your OpenCV stuff it should call QCoreApplication::quit (). As it is a static slot you can connect a signal to it or call it explicitly.

ankit.ckt
  • 83
  • 4